问题描述
我是 python 的新手,正在尝试使用 matplotlib 在同一图中绘制多条线.我的 Y 轴的值存储在字典中,我在下面的代码中在 X 轴上做了相应的值
I am new to python and am trying to plot multiple lines in the same figure using matplotlib.Value of my Y axis is stored in a dictionary and I make corresponding values in X axis in my following code
我的代码是这样的:
for i in range(len(ID)):
AxisY= PlotPoints[ID[i]]
if len(AxisY)> 5:
AxisX= [len(AxisY)]
for i in range(1,len(AxisY)):
AxisX.append(AxisX[i-1]-1)
plt.plot(AxisX,AxisY)
plt.xlabel('Lead Time (in days)')
plt.ylabel('Proportation of Events Scheduled')
ax = plt.gca()
ax.invert_xaxis()
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
plt.show()
但是我得到了一个一个单独的图形一个一个.有人可以帮我弄清楚我的代码有什么问题吗?为什么我不能生成多线绘图?非常感谢!
But I am getting separate figures with single plot one by one. Can anybody help me figure our what is wrong with my code? Why can't I produce multiple line plotting? Thanks a lot!
推荐答案
这个很简单:
import matplotlib.pyplot as plt
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.legend(loc='best')
plt.show()
您可以根据需要多次添加 plt.plot
.对于线型
,首先需要指定颜色.所以对于蓝色,它是 b
.对于普通行,它是 -
.一个例子是:
You can keep adding plt.plot
as many times as you like. As for line type
, you need to first specify the color. So for blue, it's b
. And for a normal line it's -
. An example would be:
plt.plot(total_lengths, sort_times_heap, 'b-', label="Heap")
这篇关于Python中的一个图形中的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!