当一个有标签而不是另一个时如何对齐子图边界?
How to align subplots borders when one has label and not the other?
在下面的示例中,我如何让 ax2(底部)占据左侧的完整 space?
"taking full space" 我的意思是将绘图区域扩展到图形的左边界,即。还使用标签标题下方的白色space 和 ax1.
中的刻度
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def example_plot(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_ylabel('y-label', fontsize=fontsize)
ax.set_title('Title', fontsize=fontsize)
def example_plot_noY(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_yticks([])
ax.set_title('Title', fontsize=fontsize)
plt.close('all')
fig = plt.figure()
gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
example_plot(ax1)
example_plot_noY(ax2)
gs1.tight_layout(fig)
plt.show()
参见GridSpec with Varying Cell Sizes。
您可以在 gridspec 中添加另一列,该列占用上图标签的 space。如果之后调用tight layout,就不用关心它的宽度,可以是0尺寸,
gs1 = gridspec.GridSpec(2, 2, width_ratios=[0, 1])
ax1 = fig.add_subplot(gs1[0,1])
ax2 = fig.add_subplot(gs1[1,0:])
# ...
fig.tight_layout()
在下面的示例中,我如何让 ax2(底部)占据左侧的完整 space?
"taking full space" 我的意思是将绘图区域扩展到图形的左边界,即。还使用标签标题下方的白色space 和 ax1.
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def example_plot(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_ylabel('y-label', fontsize=fontsize)
ax.set_title('Title', fontsize=fontsize)
def example_plot_noY(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_yticks([])
ax.set_title('Title', fontsize=fontsize)
plt.close('all')
fig = plt.figure()
gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
example_plot(ax1)
example_plot_noY(ax2)
gs1.tight_layout(fig)
plt.show()
参见GridSpec with Varying Cell Sizes。 您可以在 gridspec 中添加另一列,该列占用上图标签的 space。如果之后调用tight layout,就不用关心它的宽度,可以是0尺寸,
gs1 = gridspec.GridSpec(2, 2, width_ratios=[0, 1])
ax1 = fig.add_subplot(gs1[0,1])
ax2 = fig.add_subplot(gs1[1,0:])
# ...
fig.tight_layout()