2

I need to produce some plots with matplotlib but I am very poor at it. I have five lists which contain 100 values each. Their values vary according to:

enter image description here

I want to be able to produce two line-and-marker charts out of them:

  1. The first plot involves List 1, 2, 3, and 4, and has two y-axes. List 1, 2, and 3 rely on the regular y-axis, while list 4 relies on the added y-axis, like the following:

enter image description here

  1. The second has to plot just List 4 and 5, but with the regular y-axis.

Do I need to turn the each list into a numpy array before going on? Anyhow, I failed to figure out how to do the plotting with matplotlib. Any help will be much appreciated. Thanks!

FaCoffee
  • 7,609
  • 28
  • 99
  • 174

2 Answers2

2

You just need to make a twinx axes to plot list 4 on a separate y axis. You can see an example here.

And here's a short script to do what you want. No need to convert to numpy arrays in this case.

import matplotlib.pyplot as plt

# Some sample lists
l1 = [0.7,0.8,0.8,0.9,0.8,0.7,0.6,0.9,1.0,0.9]
l2 = [0.2,0.3,0.1,0.0,0.2,0.1,0.3,0.1,0.2,0.1]
l3 = [0.4,0.6,0.4,0.5,0.4,0.5,0.6,0.4,0.5,0.4]

l4 = [78,87,77,65,89,98,74,94,85,73]
l5 = [16,44,14,55,34,36,76,54,43,32]

# Make a figure
fig = plt.figure()

# Make room for legend at bottom
fig.subplots_adjust(bottom=0.2)

# The axes for your lists 1-3
ax1 = fig.add_subplot(211)
# A twin axis for list 4. This shares the x axis, but has a separate y axis
ax2 = ax1.twinx()

# Plot lines 1-3
line1 = ax1.plot(l1,'bo-',label='list 1')
line2 = ax1.plot(l2,'go-',label='list 2')
line3 = ax1.plot(l3,'ro-',label='list 3')

# Plot line 4
line4 = ax2.plot(l4,'yo-',label='list 4')

# Some sensible y limits
ax1.set_ylim(0,1)
ax2.set_ylim(0,100)

# Your second subplot, for lists 4&5
ax3 = fig.add_subplot(212)

# Plot lines 4&5
ax3.plot(l4,'yo-',label='list 4')
line5 = ax3.plot(l5,'mo-',label='list 5')

# To get lines 1-5 on the same legend, we need to 
# gather all the lines together before calling legend
lines = line1+line2+line3+line4+line5
labels = [l.get_label() for l in lines]

# giving loc a tuple in axes-coords. ncol=5 for 5 columns
ax3.legend(lines, labels, loc=(0,-0.4), ncol=5)

ax3.set_xlabel('events')

# Display the figure
plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Great! And what if I wanted to move the legend under the x-axis label that I call `events`? – FaCoffee Nov 13 '15 at 11:10
  • 1
    Just gonna edit that in. in short: add `loc=(0,-0.3), ncol=5` to the legend call. I've also modified so all 5 lines appear in the same legend – tmdavison Nov 13 '15 at 11:19
  • Brilliantly useful! But I don't need them to appear on the same legend: two legends are ok, as they need be differentiated. But you can leave it the way it is. Thanks! – FaCoffee Nov 13 '15 at 11:21
  • Btw, how to add the `y label` to both subplots? They are two different labels. – FaCoffee Nov 13 '15 at 11:59
  • 1
    `ax1.set_ylabel('Left hand label, top subplot')`, `ax2.set_ylabel('Right hand label, top subplot')`, `ax3.set_ylabel('Left hand label, bottom subplot')` – tmdavison Nov 13 '15 at 12:03
  • And how can I amend the `x labels` to make them start at `1` and end at `10`, where `10` is the `len(list1)+1`? – FaCoffee Nov 13 '15 at 12:08
  • 1
    `x = range(1,len(list1)+1)`, and then when you plot: `line1=ax1.plot(x,l1,...)`, etc – tmdavison Nov 13 '15 at 12:23
0
import matplotlib.pyplot as plt

l1 = [0.1,0.2,0.3,0.5,0.8,1.7,3.6,6.9,9.0,3.9]
l2 = [100,80,70,40,30,20,10,8,5,3]

fig = plt.figure()
fig.subplots_adjust(bottom=0.2)

ax1 = fig.add_subplot(211)
ax2 = ax1.twinx()

line1 = ax1.plot(l1,'bo-',label='list 1', color="blue")
line2 = ax2.plot(l2,'yo-',label='list 2', color="green")

ax1.set_xlabel('X-axis', color="red")

ax1.set_ylabel('Y1-axis', color="blue") 
ax2.set_ylabel('Y2-axis', color="green") 

plt.show()
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Mar 20 '22 at 17:06