当一个有标签而另一个没有标签时,如何对齐子批次边界?

2024-04-24 05:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的示例中,如何让ax2(底部)占用左侧的全部空间?
我所说的“占用全部空间”是指将绘图区域扩展到图形的左侧限制,即使用标签标题下方的空白和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()

Example


Tags: importplotmatplotlibexamplefig空间pltax
1条回答
网友
1楼 · 发布于 2024-04-24 05:45:50

GridSpec with Varying Cell Sizes。 可以在gridspec中添加另一列,该列将占用上部绘图的标签空间。如果你后来调用紧凑布局,你不需要关心它的宽度,它可以是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()

enter image description here

相关问题 更多 >