字幕位置错误,字幕不可见。

4
我有五个列表,打算在两个独立的子图中绘制。在子图1中,我想要列表1、2、3和4;在子图2中,我想要列表4和5。这些是列表以及用于设置x标签的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. 我看不到/阅读图例,我希望它居中,但位于第二个子图的x标签下方
  3. 所有我的列表都有长度为10,但只绘制了9个值
  4. 可能我想要去掉第二个子图的y轴刻度
  5. 我希望图表标题“一些趋势”不与第一个子图“粘连”
我该如何改进我的图表?谢谢!

1
这不会太多了吗?它们都涉及到同一个主题,即如何正确显示双子图表。此外,一些单独的问题可以标记为“重复”,未来读者可能会遇到同样的问题... - FaCoffee
2
我在你上一个问题中向你展示了如何为图例腾出底部空间。你需要使用 fig.subplots_adjust(bottom=0.3)(你可能需要调整0.3的值)。 - tmdavison
2
你只有9分,因为你通过将xlim设置为(1,len(list)+1)来削减第一个点,因为Python从0开始索引,而不是1。 - tmdavison
2个回答

6
  1. Plot the subtitle using ax1.set_title, not plt.title (as that will plot a title on the last active subplot)

  2. I showed you in your last question how to make room for the legend at the bottom. You need to fig.subplots_adjust(bottom=0.3) (you might need to adjust the 0.3)

  3. you only have 9 points because you are cutting out the first by setting xlim to (1,len(list)+1), since python indexes from 0, not 1. Instead, create a list to plot as your x values:

    x = range(1,len(list1)+1)
    ax1.plot(x, list1)
    
  4. Use ax3.yaxis.set_ticks_position('left') to only plot the ticks on the left and turn them off on the right

  5. You can move this increasing the y argument, e.g.

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

是的,抱歉。第四点是指下部子图上的“y轴”刻度。我会重新措辞问题本身。 - FaCoffee
是的,抱歉。如果可能的话,在右边。如果不行也没关系。 - FaCoffee
谢谢。你的答案#1:如果我调用ax1.title(subtitle_string, fontsize=8),我会得到一个TypeError: 'Text' object is not callable错误,指向ax1.title()所在的行。 - FaCoffee
1
啊,是的,那应该是 ax1.set_title。再次编辑过了。 - tmdavison
1
你可以通过类似的方式在顶部留出空间,例如 fig.subplots_adjust(top=0.85,bottom=0.2) - tmdavison
显示剩余3条评论

3
  1. You can manually set the position of the suptitle as described here: http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.suptitle Just use it like this with x and y being relative coordinates of your plot (from 0 to 1):

    suptitle("Here goes the title", x=0.5, y=0.95)
    
  2. You can set the legend position with as described here: https://dev59.com/IG445IYBdhLWcg3wydFk#4701285 Try this as suggested by user 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. Your plots only show 9 values, because you x-axes start at 1 but Python's iteration starts at 0. Since you give no x values in your plot, Python plots from 0 to 9 but you limited the axes from 1 to 10. Do this:

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

    and repeat for the other plots.

  4. What do you mean by grid? Usually grid refers to the gridlines, which you do not have. If you mean the ticks you can remove them with ax2.get_yaxis().set_ticks([])

  5. Try using plt.gcf().tight_layout(). This will arrange your plot with proper whitespace everywhere in most cases.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接