【问题标题】:Sharing a yaxis label with two of three subplots in pyplot与 pyplot 中三个子图中的两个共享一个 yaxis 标签
【发布时间】:2013-12-18 14:18:27
【问题描述】:

我有下面的代码可以产生所示的情节。

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(“出现次数”),也就是说,它应该出现在第一个和第二个图的中心左侧。我该怎么做?

【问题讨论】:

  • 英文更正:你的意思是“出现次数”吗?

标签: python matplotlib


【解决方案1】:

我能想到的最好的方法是将文本添加到图形本身并将其放置在中心(图形坐标为 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()

【讨论】:

    【解决方案2】:

    您也可以手动调整位置,使用ylabely参数:

    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)
    plt.ylabel("Number of occurrence", y=-0.8)          ##  ← ← ← HERE
    
    ax1 = plt.subplot(gs[1], sharey=ax0)
    ax1.bar(range(l), Two)
    
    ax2 =  plt.subplot(gs[2])
    ax2.bar(range(l), One)
    
    plt.show()
    

    【讨论】:

      【解决方案3】:

      如果用正确的术语说,我认为这个问题更多的是创建多轴。我想向您指出我提出的问题和收到的答案,它们为您提供了为绘图创建多轴的解决方案。

      链接到 Matplotlib 中的类似问题解决方案: Using Multiple Axis

      另一个相关问题的链接是:multiple axis in matplotlib with different scales

      【讨论】:

        【解决方案4】:

        这不是一个非常复杂的解决方案,但这可行吗?替换

        plt.ylabel("Number of occurrence")
        

        plt.ylabel("Number of occurrence", verticalalignment = 'top')
        

        [编辑]

        对于我的 64 位 python (2.7.3) 版本,我需要做一些小改动

        plt.ylabel("Number of occurrence", horizontalalignment = 'right')
        

        这就是我的样子,这不是你想要的吗?

        【讨论】:

        • 这不会将标签放在前两个图的中心。
        • @DurgaDatta 我已经添加了图像在我的计算机上的样子,这是要求吗?
        • 这可以完成我的工作。但是,我的机器中没有这种图像。你把那条线放在哪里?重要吗?
        • 只需将你的 ylabel 替换为我上面的那个
        • 我在 python 2.7.3 32 位上尝试过它并且它工作,当我在 2.7.3 64 位上尝试它时它没有。如果您使用的是 64 位,请尝试使用plt.ylabel("Number of occurrence", horizontalalignment = 'right')
        猜你喜欢
        • 1970-01-01
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-01
        • 1970-01-01
        • 2020-09-14
        • 1970-01-01
        相关资源
        最近更新 更多