重叠的约束3D子图

发布于 2025-01-27 16:30:18 字数 706 浏览 3 评论 0原文

我必须调整哪些旋钮以防止这些问题:

  • 重叠的轴标签
  • 与我使用Matplotlib 3.5.1的裁剪轴标签重叠的地块,

并带有PGF后端。一些较旧版本的解决方案不再起作用。

fig, axes = plt.subplots\
    (2, 3, constrained_layout=True, subplot_kw=dict(projection="3d"))

#it = np.nditer(axes, flags=["refs_ok","multi_index"])
#for ax in it:
#    # Plot the surfaces, add row and column title annotations.
#    pass

width = 150 * 0.8 * mm
height = width * 0.65
fig.set_size_inches(width, height)
fig.savefig("something.pgf", dpi=300)
plt.close(fig)

What knobs must I tweak to prevent these problems:

  • overlapping axes labels
  • overlapping plots with cropped axes labels

I'm using matplotlib 3.5.1 with the PGF backend. Some solutions for older versions no longer work.

overlapping constrained 3d subplots

fig, axes = plt.subplots\
    (2, 3, constrained_layout=True, subplot_kw=dict(projection="3d"))

#it = np.nditer(axes, flags=["refs_ok","multi_index"])
#for ax in it:
#    # Plot the surfaces, add row and column title annotations.
#    pass

width = 150 * 0.8 * mm
height = width * 0.65
fig.set_size_inches(width, height)
fig.savefig("something.pgf", dpi=300)
plt.close(fig)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舟遥客 2025-02-03 16:30:18

摆脱受约束的布局,并使用plt.subplots_adjust(wspace =< value>,hspace =< value>)

from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=3, subplot_kw=dict(projection="3d"))

plt.subplots_adjust(wspace=0.5,hspace=0.5)

labels_x, labels_y, labels_z = [['x-axis']*3]*2, [['y-axis']*3]*2, [['z-axis']*3]*2

for i in range(len(axes)):
    for j in range(len(axes[i])):
        axes[i,j].set_xlabel(labels_x[i][j])
        axes[i,j].set_ylabel(labels_y[i][j])
        axes[i,j].set_zlabel(labels_z[i][j])
    
plt.show()

”

Getting rid of the constrained layout and using plt.subplots_adjust(wspace=<value>, hspace=<value>) worked for me

from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=3, subplot_kw=dict(projection="3d"))

plt.subplots_adjust(wspace=0.5,hspace=0.5)

labels_x, labels_y, labels_z = [['x-axis']*3]*2, [['y-axis']*3]*2, [['z-axis']*3]*2

for i in range(len(axes)):
    for j in range(len(axes[i])):
        axes[i,j].set_xlabel(labels_x[i][j])
        axes[i,j].set_ylabel(labels_y[i][j])
        axes[i,j].set_zlabel(labels_z[i][j])
    
plt.show()

Plots

止于盛夏 2025-02-03 16:30:18

虽然可以与3D投影(MPLOT3D)一起使用紧密和约束的布局,但似乎受约束的布局不了解如何填充3D tick标签,从而导致重叠或修剪标签。两个布局经理都会调整子图填充物和轴尺寸,并给定固定的数字尺寸。两者都无法将图形大小适合内容。为了通过紧密的布局这样做,请在多个迭代中从当前图形的Bbox中推断所需的图形大小。使用受约束的布局管理器时,将轴紧密的bbox和填充总和以确定任何外部空间。紧密布局调整子图(轴大小和图形填充),并支持“ H_PAD”和“ W_PAD”参数。约束布局调节轴的大小,并支持“ WSPACE”,“ HSPACE”,“ W_PAD”和“ H_PAD”参数。紧密的布局将子图挤入一个紧密的组中,然后以可用空间为中心。受约束的布局在所有可用空间上均匀分布了子图。不管布局管理器如何,如果滴答重叠或凝聚得太紧,请将tick定位器切换为“ maxnlocator”,以获取一些较小的“ n”。

根问题是3D投影式标签是空的,直到绘制完整的画布为止。受约束布局管理器执行的“ _DRAW_DISABLE”绘图不足以触发tick标签。如果跟踪轴紧密的bbox,您会注意到它们不包含标签,直到呼叫“ Fig.Canvas.Draw”,在此之前,tick标签只是“文本(0,0,'')”。确保在尺寸大小后包含此调用,然后将布局按预期限制。鉴于固定的宽度,请根据美学设置高度或用填充轴将轴紧密的boxs汇总以确定最小可能的高度。

While both tight and constrained layouts can be used with 3d projection (mplot3d) it seems that constrained layout does not understand how to pad 3d tick labels, leading to overlapping or trimmed labels. Both layout managers adjust subplot padding and axes size given a fixed figure size. Neither can fit the figure size to the contents. To do so with tight layout, extrapolate the desired figure size from the current figure tight bbox over multiple iterations. When using the constrained layout manager, sum the axes tight bboxes and padding to determine any extraneous space. Tight layout adjusts subplotpars (axes size and figure padding) and supports "h_pad" and "w_pad" parameters. Constrained layout adjusts axes size and supports "wspace", "hspace", "w_pad", and "h_pad" parameters. The tight layout squeezes subplots into a tight group which is then centered in the available space. The constrained layout distributes subplots evenly across all available space. Regardless of the layout manager, if the ticks overlap or are clustered too tightly, switch the tick locator to "MaxNLocator" for some smaller "n".

The root problem is that the 3d projection tick labels are empty until a full canvas draw. The "_draw_disabled" draw performed by the constrained layout manager isn't sufficient to trigger the tick labels. If you trace the axes tight bbox you'll notice they don't include the labels until after a call to "fig.canvas.draw", before then the tick labels are just "Text(0, 0, '')". Be sure to include this call after sizing the figure and then the layout will be constrained as expected. Given a fixed figure width, set the height based on aesthetics or sum the axes tight bboxes with padding to determine the minimum possible height.

黎歌 2025-02-03 16:30:18

以防万一您将其嵌入PYQT GUI中,使用MPL工具栏,此订单可能会有所帮助:

fig.canvas.draw()
fig.set_constrained_layout(True)
# update your ax as needed here
ax.set_anchor('C') # or whichever position desired
plt.draw()
toolbar.update()

Just in case you are embeding this in a PyQt Gui, with the mpl toolbar this order might be helpful:

fig.canvas.draw()
fig.set_constrained_layout(True)
# update your ax as needed here
ax.set_anchor('C') # or whichever position desired
plt.draw()
toolbar.update()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文