[英]Matplotlib: how to plot data from lists, adding two y-axes?
您只需要制作一個twinx
軸即可在單獨的 y 軸上繪制list 4
。 您可以在此處查看示例。
這是一個簡短的腳本來做你想做的事。 在這種情況下無需轉換為numpy
數組。
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()
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.