I'm plotting a Pandas DataFrame with a few lines, each in a specific color (specified by rgb value). I'm looking for a way to make my code more readable by assigning the plot line colors directly to DataFrame column names instead of listing them in sequence.
I know I can do this:
import pandas as pd
df = pd.DataFrame(columns=['red zero line', 'blue one line'], data=[[0, 1], [0, 1]])
df.plot(colors = ['#BB0000', '#0000BB']) # red and blue
but with a lot more than two lines, I'd really like to be able to specify the colors by column header, to make the code easy to maintain. Such as this:
df.plot(colors = {'red zero line': '#FF0000', 'blue one line': '#0000FF'})
The colors keyword can't actually be a dictionary though. (Technically it's type-converted to list, which yields a list of the column labels.)
I understand that pd.DataFrame.plot
inherits from matplotlib.pyplot.plot
but I can't find the documentation for the colors
keyword. Neither of the documentations for the two methods lists such a keyword.
color
argument todf.plot()
, and notcolors
is the one that works.