简体   繁体   English

绘制许多具有不同比例尺的y轴:如何紧缩图形

[英]Plot with many y-axis in different scales: how to tight figure

I am following this discussion to produce a plot in which report different data sets whose x-axis is the same, but y-axis has different scales. 我正在按照此讨论生成一个图表,在该图表中报告x轴相同但y轴具有不同比例的不同数据集。 I can reproduce my plot, similar to this: 我可以复制我的剧情,类似于:

在此处输入图片说明

With some code: 带有一些代码:

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[-1].spines['right'].set_position(('axes', 1.2))

# To make the border of the right-most axis visible, we need to turn the frame
# on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

But it happens that, by side the figure, there is plenty of empty room, so that the figure is "squeezed" in 3/4 of the sheet, instead of being spread over the whole page. 但是,碰巧在图形旁边有很多空白空间,因此图形被“挤压”在纸张的3/4中,而不是散布在整个页面上。

Why does this happen? 为什么会这样? And what could be a possible way to avoid it? 避免这种情况的可能方法是什么?

Normally you would get this: 通常,您会得到以下信息:

在此处输入图片说明

You may want to call fig.tight_layout() before calling plt.show() . 您可能需要在调用fig.tight_layout()之前先调用plt.show()

在此处输入图片说明

You also want to fiddle with the axis offset ( 1.13 instead of 1.2 ): 您还想摆弄轴偏移( 1.13而不是1.2 ):

axes[-1].spines['right'].set_position(('axes', 1.13))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM