I am trying to plot some data in pandas and the inbuilt plot function conveniently plots one line per column. What I want to do is to manually assign each line a color based
What is happening here?
The keyword argument color is inherited from matplotlib.pyplot.plot(). The details in the documentation don't make it clear that you can put in a list of colors when plotting. Given that color is a keyword argument from matplotlib, I'd recommend not using a Pandas Series to hold the color values.
How can I make this work?
Use a list instead of a Series. If you were using a Series with an index meant to match the columns of your DataFrame to specific colors, you will need to sort the Series first. If the columns are not in order, you will need to sort the columns as well.
# Option 1
s = s.sort_index()
df.plot(color = s.values) # as per Fiabetto's answer
# Option 2
df.plot(color = ['c', 'y']) # other method
Try:
df.plot(color = s.values)
this will assign the colors no matter the scale of the index.
EDIT:
I tried with three columns:
df = pd.DataFrame({'1': [1, 2, 3, 4], '2': [1, 2, 1, 2], '3': [4, 3, 2, 1]})
s = pd.Series(['c','y','r'], index=[1,3,2])
df.plot(color = s.sort_index().values)
and sorting the Series it works.