Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Please, help me to plot two lists on the same graph. The lines should be of different colors. Here is the code I tried:

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()
like image 946
Alberto Alvarez Avatar asked Jul 17 '17 23:07

Alberto Alvarez


2 Answers

You should use plt.plot twice to plot two lines.

I don't know what is your X axis but obviously you should create another array/list to be your X value.

Then use plt.plot(x_value,forecast, c='color-you-want') and plt.plot(x_value,train_z, c='another-color-you-want').

. Please refer to the pyplot documentation for more details.

like image 179
Anna Yu Avatar answered Oct 25 '22 02:10

Anna Yu


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

It's very easy, Hope this sample code will help.

like image 27
subrata Avatar answered Oct 25 '22 02:10

subrata