繁体   English   中英

如何在同一张图中绘制两个具有不同颜色的列表?

[英]How to plot two list in the same graph, but with different colors?

请帮我在同一张图上绘制两个列表。 线条应为不同的颜色。 这是我尝试的代码:

import matplotlib.pyplot as plt 
train_X = [1,2,3,4,5] 
train_Y = [10, 20, 30, 40, 50] 
train_Z = [10, 20, 30, 40, 50,25] 
alpha = float(input("Input alpha: ")) 
forecast = [] for x in range(0, len(train_X)+1):  
    if x==0:       
        forecast.append(train_Y[0])  
    else:  
        forecast.append(alpha*train_Y[x-1] + (1 - alpha) * forecast[x-1])
plt.plot(forecast,train_Z,'g') 
plt.show()

您应该使用plt.plot两次绘制两条线。

我不知道您的X轴是什么,但是显然您应该创建另一个数组/列表作为X值。

然后使用plt.plot(x_value,forecast, c='color-you-want')plt.plot(x_value,train_z, c='another-color-you-want')

请参阅pyplot文档以获取更多详细信息。

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,12,15],'g*', [1,4,9,16], 'ro')
plt.show()

这很容易,希望此示例代码会有所帮助。

从另一个答案窃取借口,这似乎起作用:

# plt.plot(forecast,train_Z,'g') # replace this line, with the following for loop

for x1, x2, y1,y2 in zip(forecast, forecast[1:], train_Z, train_Z[1:]):
    if y1 > y2:
        plt.plot([x1, x2], [y1, y2], 'r')
    elif y1 < y2:
        plt.plot([x1, x2], [y1, y2], 'g')
    else:
        plt.plot([x1, x2], [y1, y2], 'b') # only visible if slope is zero

plt.show()

在此处输入图片说明

其他答案: python / matplotlib-多色线

当然,请使用https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot颜色列表中的任何其他值替换'r''g''b'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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