【问题标题】:Matplotlib: subtitle in the wrong position and legend not visibleMatplotlib:字幕位置错误,图例不可见
【发布时间】:2016-02-15 00:47:45
【问题描述】:

我有五个列表,我打算将它们绘制在两个单独的子图中。在 subplot 1 我想要列表 1、2、3 和 4;在子图 2 中,我想要列表 4 和 5。这些是列表和用于设置 x labelevent_index

event_index=['event 1','event 2','event 3','event 4','event 5','event 6','event 7','event 8','event 9','event 10']
list1 = [0.7,0.8,0.8,0.9,0.8,0.7,0.6,0.9,1.0,0.9]
list2 = [0.2,0.3,0.1,0.0,0.2,0.1,0.3,0.1,0.2,0.1]
list3 = [0.4,0.6,0.4,0.5,0.4,0.5,0.6,0.4,0.5,0.4]
list4 = [78,87,77,65,89,98,74,94,85,73]
list5 = [16,44,14,55,34,36,76,54,43,32]

要生成两个子图,我使用以下代码:

fig = plt.figure() #Creates a new figure
ax1 = fig.add_subplot(211) #First subplot: list 1,2,3, and 4
ax2 = ax1.twinx() #Creates a twin y-axis for plotting the values of list 4
line1 = ax1.plot(list1,'bo-',label='list1') #Plotting list1
line2 = ax1.plot(list2,'go-',label='list2') #Plotting list2
line3 = ax1.plot(list3,'ro-',label='list3') #Plotting list3
line4 = ax2.plot(list4,'yo-',label='list4') #Plotting list4
ax1.set_ylim(0,1)
ax1.set_xlim(1, len(event_index)+1)
ax1.set_ylabel('Some values',fontsize=12)
ax2.set_ylabel('% values',fontsize=12)
ax2.set_ylim(0,100)
ax2.set_xlim(1, len(event_index)+1)
ax3 = fig.add_subplot(212) #Second subplot: list 4 and 5
ax3.set_xlim(1, len(event_index)+1)
ax3.set_ylabel('% values',fontsize=12)
#Plotting Footprint % and Critical Cells %
ax3.plot(list4,'yo-',label='list4')
line5 = ax3.plot(list5,'mo-',label='list5')
#Assigning labels
lines = line1+line2+line3+line4+line5
labels = [l.get_label() for l in lines]
ax3.legend(lines, labels, loc=(0,-0.4), ncol=5) #The legend location. All five series are in the same legend.
ax3.set_xlabel('events')
title_string=('Some trends')
subtitle_string=('Upper panel: list 1, 2, 3, and 4 | Lower panel: list 4 and 5')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=8)
fig.tight_layout()
plt.show()

我得到的是:

几个问题:

  1. 字幕不在第一个子情节之上,而是在第二个子情节之上
  2. 我看不到/阅读我想要居中的图例,但低于子图 2 的 x labels
  3. 我的所有列表都有 len=10,但仅绘制了 9
  4. 可能,我想去掉第二个子图中的 y-axis 标记
  5. 我希望图表标题一些趋势不要“粘”到第一个子图

如何改进我的图表?谢谢!

【问题讨论】:

  • 是不是太过分了?它们都与同一个主题有关,即如何正确显示 2-subplot 图表。此外,一些单独的问题可以标记为duplicate,未来的读者可能会遇到同样的问题......
  • 我在上一个问题中向您展示了如何为底部的图例腾出空间。你需要fig.subplots_adjust(bottom=0.3)(你可能需要调整0.3)
  • 你只有 9 分,因为你通过将xlim 设置为(1,len(list)+1) 来删除第一个,因为 python 索引从 0,而不是 1

标签: python matplotlib subplot subtitle


【解决方案1】:
  1. 使用ax1.set_title而不是plt.title绘制字幕(因为这将在最后一个活动子图上绘制标题)

  2. 我在上一个问题中向您展示了如何为底部的图例腾出空间。你需要 fig.subplots_adjust(bottom=0.3) (你可能需要调整 0.3)

  3. 你只有 9 个点,因为你通过将 xlim 设置为 (1,len(list)+1) 来删除第一个点,因为 python 索引从 0,而不是 1。相反,创建一个列表来绘制你的 x 值:

    x = range(1,len(list1)+1)
    ax1.plot(x, list1)
    
  4. 使用ax3.yaxis.set_ticks_position('left') 仅在左侧绘制刻度并在右侧关闭它们

  5. 你可以移动这个增加y参数,例如

    plt.suptitle(title_string, y=1.05, fontsize=17)
    

【讨论】:

  • 是的,对不起。第 4 点指的是下方子图上的y-axis 刻度。我将改写问题本身。
  • 在两边,还是在左边?
  • 是的,对不起。如果可能,在右侧。如果没有,不用担心。
  • 啊,是的,应该是ax1.set_title。再次编辑
  • 与在底部腾出空间的方式相同,您可以在顶部腾出空间,例如fig.subplots_adjust(top=0.85,bottom=0.2)
【解决方案2】:
  1. 您可以手动设置字幕的位置,如下所述:http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.suptitle 只需像这样使用它,x 和 y 是绘图的相对坐标(从 0 到 1):

    suptitle("Here goes the title", x=0.5, y=0.95)
    
  2. 您可以按照此处所述设置图例位置:https://stackoverflow.com/a/4701285/5528308 按照用户 Joe Kington 的建议尝试此操作

    # Shrink current axis's height by 10% on the bottom
    box = ax.get_position()
    ax.set_position([box.x0, box.y0 + box.height * 0.1,
             box.width, box.height * 0.9])
    
    # Put a legend below current axis
    ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=5)
    
  3. 您的绘图仅显示 9 个值,因为您的 x 轴从 1 开始,但 Python 的迭代从 0 开始。由于您在绘图中没有给出 x 值,Python 绘图从 0 到 9,但您将轴限制在 1 到 10 . 这样做:

    x_vals = np.linspace(1,10,10)
    line1 = ax1.plot(x_vals, list1,'bo-',label='list1')
    

    然后重复其他地块。

  4. 网格是什么意思?通常网格是指您没有的网格线。如果您指的是刻度线,您可以使用 ax2.get_yaxis().set_ticks([])

  5. 删除它们
  6. 尝试使用plt.gcf().tight_layout()。在大多数情况下,这将使您的情节在任何地方都有适当的空白。

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多