Skip to content Skip to sidebar Skip to footer

Pandas/matplotlib Bar Chart With Colors Defined By Column

I am trying to make a bar chart in python, and color bars by a data frame column, this is exceedingly easy in R ggplot2, so I really do who not understand why it is so hard in matp

Solution 1:

Try this:

data.plot('name','value',color=['r', 'g', 'b'],kind='bar')

You can give any combination of colors as a list to the color argument. If there are more bars than number of colors, the colors will just recycle.

enter image description here

I can also strongly recommend the excellent brewer2mpl library. It provides aesthetically pleasing color choices. The code looks like this:

import brewer2mpl
bmap = brewer2mpl.get_map('Set2','qualitative',3,reverse=True)
colors = bmap.mpl_colors
data.plot('name','value',color=colors,kind='bar')

resulting in:

enter image description here

You can get brewer2mpl from here: https://pypi.python.org/pypi/brewer2mpl/1.4


Post a Comment for "Pandas/matplotlib Bar Chart With Colors Defined By Column"