如何为子图设置公共轴标签
我有以下图:
import matplotlib.pyplot as plt
fig2 = plt.figure()
ax3 = fig2.add_subplot(2,1,1)
ax4 = fig2.add_subplot(2,1,2)
ax4.loglog(x1, y1)
ax3.loglog(x2, y2)
ax3.set_ylabel('hello')
我想创建跨越两个子图的轴标签和标题。例如,由于两个图具有相同的轴,因此我只需要一组 xlabel
和 ylabel
。不过,我确实希望每个子图都有不同的标题。
我怎样才能做到这一点?
I have the following plot:
import matplotlib.pyplot as plt
fig2 = plt.figure()
ax3 = fig2.add_subplot(2,1,1)
ax4 = fig2.add_subplot(2,1,2)
ax4.loglog(x1, y1)
ax3.loglog(x2, y2)
ax3.set_ylabel('hello')
I want to create axes labels and titles that span on both subplots. For example, since both plots have identical axes, I only need one set of xlabel
and ylabel
. I do want different titles for each subplot though.
How can I achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以创建一个覆盖两个子图的大子图,然后设置公共标签。
另一种方法是使用Fig.text()直接设置公共标签的位置。
You can create a big subplot that covers the two subplots and then set the common labels.
Another way is using fig.text() to set the locations of the common labels directly.
使用
子图
的一种简单方法:One simple way using
subplots
:matplotlib 3.4.0 中的新增功能
现在有内置方法来设置公共轴标签:
supxlabel
supylabel
重现 OP 的
loglog
图(通用标签但单独标题):New in matplotlib 3.4.0
There are now built-in methods to set common axis labels:
supxlabel
supylabel
To reproduce OP's
loglog
plots (common labels but separate titles):plt.setp()
将完成这项工作:plt.setp()
will do the job:如果您不想导出矢量图形或者您已将 matplotlib 后端设置为忽略无色轴,那么 Wen-wei Liao 的答案很好;否则隐藏的轴将显示在导出的图形中。
我的答案
suplabel
与使用fig.text
函数的fig.suptitle
类似。因此,没有斧头艺术家被创造并变得无色。但是,如果您尝试多次调用它,您将会看到文本相互叠加(
fig.suptitle
也是如此)。 Wen-wei Liao 的答案不是,因为如果已经创建了fig.add_subplot(111)
将返回相同的 Axes 对象。我的函数也可以在创建绘图后调用。
Wen-wei Liao's answer is good if you are not trying to export vector graphics or that you have set up your matplotlib backends to ignore colorless axes; otherwise the hidden axes would show up in the exported graphic.
My answer
suplabel
here is similar to thefig.suptitle
which uses thefig.text
function. Therefore there is no axes artist being created and made colorless.However, if you try to call it multiple times you will get text added on top of each other (as
fig.suptitle
does too). Wen-wei Liao's answer doesn't, becausefig.add_subplot(111)
will return the same Axes object if it is already created.My function can also be called after the plots have been created.
这是一个解决方案,您可以设置其中一个图的 ylabel 并调整它的位置,使其垂直居中。这样您就可以避免 KYC 提到的问题。
Here is a solution where you set the ylabel of one of the plots and adjust the position of it so it is centered vertically. This way you avoid problems mentioned by KYC.
当 yticks 很大时,其他答案中的方法将无法正常工作。 ylabel 要么与刻度重叠,要么被剪裁在左侧,要么完全不可见/在图形之外。
我修改了 Hagne 的答案,因此它适用于 xlabel 和 ylabel 的超过 1 列子图,并且它会移动绘图以保持 ylabel 在图中可见。
它适用于以下示例,而 Hagne 的答案不会绘制 ylabel (因为它在画布之外)并且 KYC 的 ylabel 与刻度标签重叠:
或者,如果您对无色轴没问题,我已经修改了 Julian Chen 的解决方案,以便ylabel 不会与刻度标签重叠。
基本上,我们只需设置无色的 ylims,使其与子图的最大 ylims 匹配,这样无色刻度标签就会为 ylabel 设置正确的位置。
同样,我们必须缩小绘图以防止剪切。在这里,我对要缩小的数量进行了硬编码,但是您可以尝试找到适合您的数字或像上面的方法一样计算它。
The methods in the other answers will not work properly when the yticks are large. The ylabel will either overlap with ticks, be clipped on the left or completely invisible/outside of the figure.
I've modified Hagne's answer so it works with more than 1 column of subplots, for both xlabel and ylabel, and it shifts the plot to keep the ylabel visible in the figure.
It works for the following example, while Hagne's answer won't draw ylabel (since it's outside of the canvas) and KYC's ylabel overlaps with the tick labels:
Alternatively, if you are fine with colorless axis, I've modified Julian Chen's solution so ylabel won't overlap with tick labels.
Basically, we just have to set ylims of the colorless so it matches the largest ylims of the subplots so the colorless tick labels sets the correct location for the ylabel.
Again, we have to shrink the plot to prevent clipping. Here I've hard coded the amount to shrink, but you can play around to find a number that works for you or calculate it like in the method above.
您可以在轴中使用“set”,如下所示:
You could use "set" in axes as follows: