Matplotlib:字幕位置错误,图例不可见

2024-09-27 07:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有五张单子,我打算分两小段列出。在子批次1中,我想要列表1、2、3和4;在子批次2中,我想要列表4和5。这些是用于设置x label的列表和event_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()

我得到的是: enter image description here

几个问题:

  1. 副标题不在第一个子块的上方,而在第二个子块的上方
  2. 我看不到/读不到图例,我想把它居中,但是在子块2的x labels下面
  3. 我的所有列表都有len=10,但只绘制9
  4. 可能,我想去掉第二个子块中的y-axis记号
  5. 我希望图表标题“某些趋势”不要“粘”在第一个子批次上

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


Tags: andevent列表indexplotplottinglabellist
2条回答
  1. 您可以手动设置suptitle的位置,如下所述: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. 尝试使用plt.gcf().tight_layout()。在大多数情况下,这将用适当的空格来排列你的绘图。

  1. 使用ax1.set_title而不是plt.title绘制字幕(因为这将在最后一个活动子块上绘制标题)

  2. 我在你的最后一个问题中向你展示了如何为底部的传奇腾出空间。您需要调整子块(底部=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)
    

相关问题 更多 >

    热门问题