Issue
I'm trying to draw a subplot of a sample problem in 2 rows as shown in Fig.01. The first row of the subplot is single column and should be centered. Also, in the Fig. 01, how to make the color bar same size as of the plots of second row?.
I've two questions: A) How to center the subplot at row 1 & B) Why the rightmost plot is smaller than other plots of the 2nd row? Can anybody please help how to solve these questions? Thanks in advance.
To produce this plot, I've used the following script:
fig, ax = plt.subplots(figsize=(8,4), dpi=100)
##*******==SUBPLOTTING= # creating grid for subplots*************************************************
degree = [0,30,45,60,90]
for deg_num in range(0,len(degree)):
............
............
ax1 = plt.subplot2grid(shape=(2, len(degree)), loc=(0, 0), colspan=3)
for i, label in enumerate(labels):
ax1.scatter(arr_x[i], arr_y[i],s=60, c='r', marker='o', cmap=None, norm=None)
text_object = ax1.annotate(label, xy=(arr_x[i], arr_y[i]), xytext=(1,-17), textcoords='offset points',ha='center')
for x,y in zip(arr_x, arr_y):
label = f"({x},{y})"
ax1.annotate(label, xy=(x,y), xytext=(8,5), textcoords='offset points',ha='center')
ax1.set_xlim(np.min(arr_x)-2,np.max(arr_x)+2)
ax1.set_ylim(np.min(arr_y)-2,np.max(arr_y)+2)
ax1.set_xlabel(" $X$-axis", fontsize=18)
ax1.set_ylabel("$Y$-axis", fontsize=18)
ax1.set_title(f'Sensor position in $X-Y$ coordinates', loc='left')
ax1.set_position([.32,0.54,0.4,0.4])
ax2 = plt.subplot2grid(shape=(2, len(degree)), loc=(1, deg_num), colspan=1)
##===============================================
c = ax2.imshow(spec, cmap='seismic', vmin=spec.min(), vmax=spec.max(),
extent=extent, interpolation='nearest', origin='lower') #
ax2.set_xlabel("Wavenumber, $K_x$ [rad/m]", fontsize=12)
ax2.set_ylabel("Wavenumber,$K_y$ [rad/m]", fontsize=12)
ax2.set_title(f'$\phi$={theta} deg. @{jj}Hz') # 'Channel %d' %i
plt.show()
cbar = fig.colorbar(c,fraction=0.046, pad=0.04)
cbar.set_label(f'Spatial Spectrum at {jj}Hz', rotation=90, fontsize=12, labelpad=1)# weight="bold")
Edit:
By setting the position ax1.set_position([.32,0.54,0.4,0.4])
the figure in first row is centered as in Fig. 02. Still the right most picture of 2nd row is smaller than other subplots. CAn anybody suggest how to fix this issue?
Solution
Here, I've found a solution to this issue. It is not generalized but it serves the purpose. For the question A) Set the position of ax1 as ax1.set_position([.32,0.54,0.4,0.4])
to make the centered plot of the first row of the grid subplot.
For Q. B) I've used add_axes
as in cax = fig.add_axes([ax2.get_position().x1+0.01,ax2.get_position().y0,0.008,ax2.get_position().height])
that create new axes for the colorbar. Here all the subplots are equal size in 2nd row. The figure then looks like as Fig. ** . The full code is given below:
fig, ax = plt.subplots(figsize=(12,8), dpi=100)
##**********************************************************************************
degree = [0,30,45,60,90]
for deg_num in range(0,len(degree)):
............
............
ax1 = plt.subplot2grid(shape=(2, len(degree)), loc=(0, 0), colspan=3)
for i, label in enumerate(labels):
ax1.scatter(arr_x[i], arr_y[i],s=60, c='r', marker='o', cmap=None, norm=None)
text_object = ax1.annotate(label, xy=(arr_x[i], arr_y[i]), xytext=(1,-17), textcoords='offset points',ha='center')
for x,y in zip(arr_x, arr_y):
label = f"({x},{y})"
ax1.annotate(label, xy=(x,y), xytext=(8,5), textcoords='offset points',ha='center')
ax1.set_xlim(np.min(arr_x)-2,np.max(arr_x)+2)
ax1.set_ylim(np.min(arr_y)-2,np.max(arr_y)+2)
ax1.set_xlabel(" $X$-axis", fontsize=18)
ax1.set_ylabel("$Y$-axis", fontsize=18)
ax1.set_title(f'Sensor position in $X-Y$ coordinates', loc='left')
ax1.set_position([.32,0.54,0.4,0.4])
##===============================================
ax2 = plt.subplot2grid(shape=(2, len(degree)), loc=(1, deg_num), colspan=1)
c = ax2.imshow(spec, cmap='seismic', vmin=spec.min(), vmax=spec.max(),
extent=extent, interpolation='nearest', origin='lower') #
ax2.set_xlabel("Wavenumber, $K_x$ [rad/m]", fontsize=12)
if deg_num < 1:
ax2.set_ylabel("Wavenumber,$K_y$ [rad/m]", fontsize=12)
ax2.set_xlabel("Wavenumber, $K_x$ [rad/m]", fontsize=12)
else:
ax2.set_xlabel("$K_x$ [rad/m]", fontsize=12)
ax2.set_title(f'$\phi$={theta} deg. @{jj}Hz')
x = kxx
y = kyy
ax2.plot(x, y, marker="o", markersize=5, markeredgecolor="yellow", markerfacecolor="yellow")
plt.show()
cax = fig.add_axes([ax2.get_position().x1+0.01,ax2.get_position().y0,0.008,ax2.get_position().height])
cbar=fig.colorbar(c, cax=cax)
cbar.set_label(f'Spatial Spectrum at {jj}Hz', rotation=90, fontsize=12, labelpad=1)
Answered By - Alan22
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.