gpt4 book ai didi

python - Matplotlib 固定图形大小和子图位置

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:06 25 4
gpt4 key购买 nike

我有一个关于 matplotlib 中子图大小的问题打扰您。我需要创建一个固定大小的图形,由单行中的 3 个子图组成。出于“编辑原因”,我需要修复图形的大小,但我也想修复子图的大小和位置,而不影响图形大小(第三个子图必须比前两个子图窄)。

我尝试使用 GridSpec 但没有成功。我还尝试使用“figsize”修复图形大小并使用 add_axes 作为子图,但是根据子图的相对大小,图形和子图的整体大小会发生变化。

使用 gnuplot 时,可以对子图使用“设置原点”和“设置大小”。我们在 matplotlib 中有类似的东西吗?

最佳答案

有这样的事吗?更改宽度比将更改各个子图的大小。

fig, [ax1, ax2, ax3] = plt.subplots(1,3, gridspec_kw = {'width_ratios':[3, 2, 1]}, figsize=(10,10))

plt.show()
<小时/>

如果您想对尺寸有更多控制,您也可以使用轴。它仍然是相对的,但现在是整个图形大小的一小部分。

import matplotlib.pyplot as plt

# use plt.Axes(figure, [left, bottom, width, height])
# where each value in the frame is between 0 and 1

# left
figure = plt.figure(figsize=(10,3))
ax1 = plt.Axes(figure, [.1, .1, .25, .80])
figure.add_axes(ax1)
ax1.plot([1, 2, 3], [1, 2, 3])

# middle
ax2 = plt.Axes(figure, [.4, .1, .25, .80])
figure.add_axes(ax2)
ax2.plot([1, 2, 3], [1, 2, 3])

# right
ax3= plt.Axes(figure, [.7, .1, .25, .80])
figure.add_axes(ax3)
ax3.plot([1, 2, 3], [1, 2, 3])

plt.show()

关于python - Matplotlib 固定图形大小和子图位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com