与 pyplot 中的三个子图中的两个共享 yaxis 标签 [英] Sharing a yaxis label with two of three subplots in pyplot

查看:70
本文介绍了与 pyplot 中的三个子图中的两个共享 yaxis 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以生成显示的图.

I have the following code which produces the plot shown.

mport matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec
import numpy as np

One = range(1,10)
Two = range(5, 14) 
l = len(One)
fig = plt.figure(figsize=(10,6))
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3]) 

ax0 = plt.subplot(gs[0])
ax0.bar(range(l), Two)
plt.ylabel("Number of occurrence")

ax1 = plt.subplot(gs[1], sharey=ax0)
ax1.bar(range(l), Two)

ax2 =  plt.subplot(gs[2])
ax2.bar(range(l), One)

plt.show()

我希望ylabel(出现次数")在第一和第二个图之间共享,也就是说,它应该出现在第一和第二个图的中心.我该怎么办?

I want the ylabel (" Number of occurrence") to be shared between first and second plot, that is, it should occur in the center left of first and second plot. How do I do that?

推荐答案

我能想到的最好方法是将文本添加到图形本身并将其放置在中心(图形坐标为 0.5)像这样

The best way I can think of is to add the text to the figure itself and position it at the center (figure coordinates of 0.5) like so

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

One = range(1,10)
Two = range(5, 14)
l = len(One)
fig = plt.figure(figsize=(10,6))
gs = gridspec.GridSpec(3, 1, height_ratios=[5, 3, 3])

ax0 = plt.subplot(gs[0])
ax0.bar(range(l), Two)

ax1 = plt.subplot(gs[1], sharey=ax0)
ax1.bar(range(l), Two)

ax2 =  plt.subplot(gs[2])
ax2.bar(range(l), One)

fig.text(0.075, 0.5, "Number of occurrence", rotation="vertical", va="center")

plt.show()

这篇关于与 pyplot 中的三个子图中的两个共享 yaxis 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆