I have five lists which I intend to plot in two separate subplots. In subplot 1 I want list 1, 2, 3 and 4; in subplot 2 I want list 4 and 5. These are the lists and the event_index
used to set the 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]
To produce the two subplots I use the following code:
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()
A few problems:
x labels
of subplot 2len=10
, but only 9
values are plottedy-axis
ticks in the second subplotHow could I improve my chart? Thanks!
Plot the subtitle using ax1.set_title
, not plt.title
(as that will plot a title on the last active subplot)
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)
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)
Use ax3.yaxis.set_ticks_position('left')
to only plot the ticks on the left and turn them off on the right
You can move this increasing the y
argument, e.g.
plt.suptitle(title_string, y=1.05, fontsize=17)