pythonmatplotlibpolar-coordinatescontourf

How do I move the cartesian subfigure axes I added to a polar plot closer together?


I've managed to successfully make a figure with three polar plots and have added cartesian axes. The problem I have is that the final figure has a lot of whitespace in between the axes, which I would like to reduce.

I think the problem is to do with the removing of the polar axes and adding of the cartesian axes meaning I'm not adjusting the whitespace in the right place.

import numpy as np
import matplotlib.pyplot as plt

r_out = 6.0
level = np.linspace(-100,100,30)

axis_text = [r"$t/t_A = 0$", r"$t/t_A = 30$", r"$t/t_A = 60$"]

fig, [ax1, ax2, ax3] = plt.subplots(1,3,figsize=(15,4), subplot_kw=dict(polar=True))
fig.subplots_adjust(bottom=0.15, right=0.85) # controls placement of figures but not space in between
axs = [ax1, ax2, ax3]

r = np.linspace(0.5, r_out, 20)
theta = np.linspace(-np.pi, np.pi, 20)
data_to_plot = np.array([[r[i]**2 * np.sin(theta[j]) for i in range(np.size(r))] for j in range(np.size(theta))])
R, T = np.meshgrid(r, theta)
X, Y = R*np.cos(T), R*np.sin(T)

r_lim = (-r_out-0.1, r_out+0.1)
for i in range(len(axs)):
    ax = axs[i]
    ax.axis('off')
    new_axis = fig.add_axes(ax.get_position().bounds,ylim=r_lim,xlim=r_lim,aspect='equal')
    cont = new_axis.contourf(X, Y, np.squeeze(data_to_plot), levels=level, cmap='coolwarm')
    if i == 0:
        new_axis.set_ylabel(r"$y$", fontsize=18)
    new_axis.set_xlabel(r"$x$", fontsize=18)
    if i != 0:
        new_axis.set_yticklabels([])
        new_axis.set_yticks([])
    new_axis.tick_params(axis="both", labelsize=18)
    new_axis.set_title(axis_text[i], fontsize=20)

fig.subplots_adjust(right=0.89) # Controls fitting in of colorbar

cbar = fig.colorbar(cont, ax=axs[2])
cbar.set_ticks(np.linspace(level.min(), level.max(), 5))
cbar.ax.tick_params(labelsize=18)
cbar.ax.set_ylabel(r"$u_r$", fontsize = 22)

As in this question, I've tried adjusting the whitespace using the wspace argument in fig.subplots_adjust() at both places it's in the code. This didn't appear to change the figure at all.

Similarly, the layout = "compressed" argument for plt.subplots() also doesn't work as in this question.

Again, I think those don't work for me because of the removed and added axes.

Ideally, I want to be able to specify something like the wspace argument so I can reduce the amount of space in between each figure.

Here's the reproducible figure from the code


Solution

  • I ran your code using the wspace parameter in fig.subplots_adjust to control the width space between the subplots. It works on my machine :-)

    Try something like:

    fig.subplots_adjust(wspace=-0.25)
    

    Both positive and negative numbers work. You will probably have to adapt your other fig.subplots_adjust calls to make everything fit however you see best.

    Full example, staying with how your code is structured right now:

    ....
    fig.subplots_adjust(bottom=0.15, right=0.85, wspace=-0.35)
    ....
    fig.subplots_adjust(right=0.85)
    ....
    

    Gives: enter image description here