Python Forum
How to plot two list on the same graph with different colors?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to plot two list on the same graph with different colors?
#1
Dear Python Users,

I tried to plot a graph from two lists on the same graph. However, what I get is that two lists are plotted against each other. How can I plot two lists on the same graph, but with different colors?


import matplotlib.pyplot as plt

train_W = [1,2,3,4,5,6]
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()
Reply
#2
If you submit two arrays as arguments, it is assumed that they are x-coordinates and y-coordinates respectively. If you use only one array, these values will be used as y-coordinates and the x-coordinates will be [0, 1, 2, ...].

You have multiple options to plot more than one function.

You can submit x and y values for each graph you want to plot:
x_list = range(6)
plt.plot(x_list, forecast, x_list, train_W, x_list, train_Z)
The colors of the lines will be different by default, but if you want to set them manually you could do that like this:
plt.plot(x_list, forecast, 'r-.', x_list, train_W, 'bx', x_list, train_Z, 'y')
But imho this code is not very easy to read. (note that 'r-.' means a dashed red line, 'bx' means blue 'x' markers with no line)

Note that the length of the arrays must be equal, or you'll run into e.g.:
Error:
ValueError: x and y must have same first dimension, but have shapes (6,) and (5,)
Also, if you want to apply other keyword arguments to the plot they'll apply to each of those lines:
plt.plot(x_list, forecast, x_list, train_W, x_list, train_Z, label='test')
plt.legend()
plt.show()
Each line in this plot will have the same entry in the in the legend.


That's why I prefer method 2: calling plot() separately for each line:
plt.plot(forecast)
plt.plot(train_Z)
plt.show()
or with x-values explicitly included:
plt.plot(x_list, forecast)
plt.plot(x_list, train_Z)
plt.show()
or with different keyword arguments for each line
plt.plot(x_list, forecast, label='forecast')
plt.plot(x_list, train_Z, label='train Z')
plt.legend()
plt.show()
You can find more info about how to use arguments for plot here: https://matplotlib.org/api/pyplot_api.ht...yplot.plot
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count image's colors very fast flash77 18 1,541 Mar-05-2024, 06:12 PM
Last Post: deanhystad
  can openpyxl read font colors mperemsky 3 1,728 May-09-2023, 11:18 AM
Last Post: MindKeeper
  first time use plot - I get empty graph korenron 6 2,070 Feb-04-2023, 02:14 PM
Last Post: deanhystad
  ANSI not working for change of text colors BliepMonster 10 3,348 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  How to do bar graph with positive and negative values different colors? Mark17 1 5,110 Jun-10-2022, 07:38 PM
Last Post: Mark17
  Unable to plot the graph as needed drunkenneo 1 6,337 May-26-2021, 11:04 AM
Last Post: cygnus_X1
  How to plot 3D graph of non numerical value? Gevni 0 2,209 Mar-05-2021, 02:50 PM
Last Post: Gevni
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,575 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Plot Graph quest_ 2 2,115 Jan-18-2021, 07:58 PM
Last Post: quest_
  How to plot intraday data of several days in one plot mistermister 3 2,896 Dec-15-2020, 07:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020