python - 与 pyplot 中的三个子图中的两个共享 yaxis 标签

标签 python matplotlib

我有以下代码生成所示的图。

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()

enter image description here

我希望在第一幅图和第二幅图之间共享 ylabel(“出现次数”),也就是说,它应该出现在第一幅图和第二幅图的左中部。我该怎么做?

最佳答案

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

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()

关于python - 与 pyplot 中的三个子图中的两个共享 yaxis 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660588/

相关文章:

python - 将预定义的数字分配给数据框中的列行值

python - 计算 pi 的小数第 N 位

python - 如何使用箱线图绘制relplot,然后仅绘制seaborn中指定的频率

python - Matplotlib:避免 X 轴拥塞

python - 在 jupyter 笔记本中同时使用 matplotlib inline 和 qt

python - 如何评论运算符之间的大赋值?

python - pyqt4程序,如何制作win安装程序

python - Pandas map 专栏到位

python - matplotlib 图例位置默认

Matplotlib - 设置刻度标签的背景颜色