FaC*_*fee 3 python matplotlib subtitle subplot
我有五个列表,我打算在两个单独的子图中绘制.在子图1中我想要列表1,2,3和4; 在子图2中我想要列表4和5.这些是列表和event_index用于设置x label.
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]
Run Code Online (Sandbox Code Playgroud)
为了生成两个子图,我使用以下代码:
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()
Run Code Online (Sandbox Code Playgroud)
一些问题:
x labels子图2的下方len=10,但只9绘制了值y-axis第二个子情节中的刻度我怎么能改善我的图表?谢谢!
使用ax1.set_title而不是绘制字幕plt.title(因为它将绘制最后一个活动子图上的标题)
我在最后一个问题中向您展示了如何为底部的传奇腾出空间.你需要fig.subplots_adjust(底部= 0.3)(你可能需要调整0.3)
你只有9分,因为你通过将xlim设置为(1,len(list)+1)来切断第一个,因为python索引从0而不是1.而是创建一个列表来绘制为x值:
x = range(1,len(list1)+1)
ax1.plot(x, list1)
Run Code Online (Sandbox Code Playgroud)使用ax3.yaxis.set_ticks_position('left')只绘制左侧的蜱虫,并把它们关在右
你可以移动这个增加y参数,例如
plt.suptitle(title_string, y=1.05, fontsize=17)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
10041 次 |
| 最近记录: |