Plotting bar chart -colors python
Question: I have a pandas dataframe that I want to plot as a barchart the data has the
following form;

    
    
    Year   ISO  Value   Color 
    2007   GBR  500     0.303
           DEU  444     0.875  
           FRA  987     0.777
    2008   GBR  658     0.303
           USA  432     0.588  
           DEU  564     0.875
    2009 ... etc
    

i tried to iterate over the data in the follow way;

    
    
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm 
    
    
    conditions=np.unique[df['Color']]
    plt.figure()
    ax=plt.gca()
    for i,cond in enumerate(conditions):
         print 'cond: ',cond
         df['Value'].plot(kind='bar', ax=ax, color=cm.Accent(float(i)/n))
    
    
         minor_XT=ax.get_xaxis().get_majorticklocs()
         df['ISO']=minor_XT
         major_XT=df.groupby(by=df.index.get_level_values(0)).first()['ISO'].tolist()
         df.__delitem__('ISO')
         plt.xticks(rotation=70)
         ax.set_xticks(minor_XT, minor=True)
         ax.set_xticklabels(df.index.get_level_values(1), minor=True, rotation=70)
         ax.tick_params(which='major', pad=45)
         _=plt.xticks(major_XT, (df.index.get_level_values(0)).unique(), rotation=0)
         plt.tight_layout()
         plt.show()
    

But it all out in one color, any suggestions as to what I am doing wrong?


Answer: Since `df['Value'].plot(kind='bar')` will plot all your bars, you do not need
to iterate over you `conditions`. Also, as `plot(kind='bar')` essentially
calls `matplotlib.pyplot.bar`, we can feed it a list of colors of the same
length as our data array, and it will color each bar using those colors.
Here's a slightly simplified example (I'll leave you to figure out the ticks
and tick labels):

    
    
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    
    df = pd.DataFrame([
        [2007,'GBR',500,0.303],
        [2007,'DEU',444,0.875],
        [2007,'FRA',987,0.777],
        [2008,'GBR',658,0.303],
        [2008,'USA',432,0.588],
        [2008,'DEU',564,0.875]],
        columns=['Year','ISO','Value','Color'])
    
    colors = cm.Accent(df['Color']/len(df['Color']))
    
    fig=plt.figure()
    ax=fig.add_subplot(111)
    
    df['Value'].plot(kind='bar',ax=ax,color=colors)
    
    plt.show()
    

[![enter image description
here](http://i.stack.imgur.com/LL3FK.png)](http://i.stack.imgur.com/LL3FK.png)

