pandas plot dataframe barplot with colors by category pandas plot dataframe barplot with colors by category pandas pandas

pandas plot dataframe barplot with colors by category


Just pass a color parameter to the plot function with a list of colors:

df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r'])

If you want to plot the value as bars and you also want the group to determine the color of the bar, use:

colors = {1: 'r', 2: 'b', 3: 'g'}df['value'].plot(kind='bar', color=[colors[i] for i in df['group']])

You can also use something like:

list(df['group'].map(colors))

Instead of the list comprehension.