Python中一个图形中的多个绘图

2022-02-21 00:00:00 python matplotlib plot

问题描述

我是Python的新手,我正在尝试使用matplotlib在同一张图中绘制多条线。 Y轴的值存储在字典中,在下面的代码中我在X轴上设置相应的值

我的代码如下:

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()

但是我得到的是单独的数字和一个接一个的图。有人能帮我找出我的代码出了什么问题吗?为什么我不能生成多行打印?非常感谢!


解决方案

这非常简单:

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任意次数。对于line type,您需要先指定颜色。所以对于蓝色,它是b。对于一条法线,它是-。例如:

plt.plot(total_lengths, sort_times_heap, 'b-', label="Heap")

相关文章