【发布时间】:2018-08-22 15:13:18
【问题描述】:
我有一个包含三个子图的图形,排列在一个列中。其中一个图在右侧使用 3 个 y 轴刺。我跟着this tutorial 在子图上插入了多个右轴刺。
我的问题是,通过添加额外的刺,图中的所有子图在 x 方向上都变小了。这改变了所有三个子图的宽度,在其他子图的右侧留下了未使用的空间。
在 y 轴上添加额外的刺时,如何仅调整一个子图的宽度?
下面是一个简化的例子,它产生了与this image中相同的问题
from matplotlib import pyplot as plt
# set up a set of three subplots
fig = plt.figure(figsize=(17, 11))
ax_1_l = fig.add_subplot(3,1,1)
ax_1_r = ax_1_l.twinx()
ax_2_l = fig.add_subplot(3,1,2)
ax_2_r = ax_2_l.twinx()
ax_3_l = fig.add_subplot(3,1,3)
ax_3_r = ax_3_l.twinx()
# add additional axes to the middle subplot as per tutorial
def make_patch_and_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
sp.set_visible(False)
ax_2_r_2 = ax_2_l.twinx()
make_patch_and_spines_invisible(ax_2_r_2)
ax_2_r_2.spines['right'].set_position(('axes', 1.05))
ax_2_r_2.spines['right'].set_visible(True)
ax_2_r_3 = ax_2_l.twinx()
make_patch_and_spines_invisible(ax_2_r_3)
ax_2_r_3.spines['right'].set_position(('axes', 1.1))
ax_2_r_3.spines['right'].set_visible(True)
# display the plots
plt.tight_layout()
plt.show()
Sample Output Image from my code
matplotlib 2.1.2 版
【问题讨论】:
-
嗨,Evan,您能否分享一下工作代码,以便我们重现该图并尝试帮助提供解决方案?
-
我将尝试将大型程序缩减为显示相同问题的较短脚本。我将不得不用一些样本替换真实数据。
-
样本数据很好(虽然我不认为这与数据有任何关系,所以也许只创建空子图就足够了..)
-
按照 DavidG 的建议,我添加了一个没有任何数据的简单代码示例
标签: python python-3.x matplotlib charts