Python中一个图形中的多个图 [英] multiple plot in one figure in Python

查看:126
本文介绍了Python中一个图形中的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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.对于line type,您需要首先指定颜色.所以对于蓝色,它是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中一个图形中的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆