python 2.7 - Pandas/matplotlib bar chart with colors defined by column -
i trying create bar chart in python, , color bars info frame column, exceedingly easy in r ggplot2, not understand why hard in matplotlib/pandas, understand how , logic assume can't hard after all
here illustration of want. did in ggplot - want define color using attributes in info frame, attribute can color string i.e. 'k','r' .. or feature of info male/female, etc.
this illustration of code tried works generate bars
import matplotlib.pyplot plt import pandas pd info = pd.dataframe({'name' : ['a','b','c'],'value' : [1,2,3], 'c' : ['b','r','b']}) data.plot('name','value',kind='bar')
but want color defined column 'c'
but when add together color='c' not work
data.plot('name','value',color='c',kind='bar',color='c')
i tried using plot.scatter , did not work either
---- update
i did more work , works more or less, have yet figure out how align labels, still why have have numeric x axis, when categorical (again ggplot2 deals on own) - ar to the lowest degree can done in code
fig, ax = plt.subplots() ax.bar(range(len(data)),data['value'],width=.5,color=data['c']) ax.set_xticks(range(len(data))) ax.set_xticklabels( (data['name'] ))
thanks always
try this:
data.plot('name','value',color=['r', 'g', 'b'],kind='bar')
you can give combination of colors list color argument. if there more bars number of colors, colors recycle.
i can recommend first-class brewer2mpl library. provides aesthetically pleasing color choices. code looks 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:
you can brewer2mpl here: https://pypi.python.org/pypi/brewer2mpl/1.4
python-2.7 matplotlib pandas
No comments:
Post a Comment