Color bars by category · Issue #7636 · pandas-dev/pandas (original) (raw)

Would it make sense to add functionality for coloring bar charts according to a categorical value in a column? It could then by be passed as an argument such as:

df.plot(kind='bar', colors=['r', 'g', 'b'], color_by=['B'])

which would lead to ['r', 'g', 'b'] being cycled throughout the values in column 'B'

Something along the lines of:

def color_by_category(series, colormap): from itertools import cycle unique_categories = set(series) color_cycle = cycle(colormap) repeat_colors = [color_cycle.next() for i in range(len(unique_categories))] category_color = {i:j for i,j in zip(unique_categories, repeat_colors)} color_array = [category_color[k] for k in series]

    return color_array