简体   繁体   English

Matplotlib:如何从列表中添加两个 y 轴的 plot 数据?

[英]Matplotlib: how to plot data from lists, adding two y-axes?

I need to produce some plots with matplotlib but I am very poor at it.我需要用matplotlib制作一些图,但我很不擅长。 I have five lists which contain 100 values each.我有五个lists ,每个列表包含100值。 Their values vary according to:它们的值因以下因素而异:

在此处输入图像描述

I want to be able to produce two line-and-marker charts out of them:我希望能够从中生成两个line-and-marker图:

  1. The first plot involves List 1, 2, 3, and 4, and has two y-axes .第一个 plot 涉及列表 1、2、3 和 4,并且有两个 y 轴 List 1, 2, and 3 rely on the regular y-axis, while list 4 relies on the added y-axis , like the following:列表 1、2 和 3 依赖于常规 y 轴,而列表 4 依赖于添加的 y 轴,如下所示:

在此处输入图像描述

  1. The second has to plot just List 4 and 5, but with the regular y-axis.第二个必须是 plot 仅列表 4 和 5,但具有常规 y 轴。

Do I need to turn the each list into a numpy array before going on?在继续之前,我是否需要将每个list转换为numpy array Anyhow, I failed to figure out how to do the plotting with matplotlib .无论如何,我没能弄清楚如何使用matplotlib进行绘图。 Any help will be much appreciated.任何帮助都感激不尽。 Thanks!谢谢!

You just need to make a twinx axes to plot list 4 on a separate y axis.您只需要制作一个twinx轴即可在单独的 y 轴上绘制list 4 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.在这种情况下无需转换为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM