pandas plot automatically assigning color to categories
<p>I want to do a bar plot my dataframe such that the categorical column ('ad') defines the colors of my bar charts.
This is my data:</p>

<pre><code>"date","shown","clicked","converted","avg_cost_per_click","total_revenue","ad"
2015-10-01,65877,2339,43,0.9,641.62,"ad_group_1"
2015-10-02,65100,2498,38,0.94,756.37,"ad_group_1"
2015-10-03,70658,2313,49,0.86,970.9,"ad_group_2"
2015-10-04,69809,2833,51,1.01,907.39,"ad_group_2"
2015-10-05,68186,2696,41,1,879.45,"ad_group_3"
2015-10-06,66864,2617,46,0.98,746.48,"ad_group_3"
2015-10-07,68227,2390,42,0.94,462.33,"ad_group_4"
2015-10-08,68520,2909,46,1.07,441.28,"ad_group_4"
2015-10-09,67250,2385,49,0.88,602.14,"ad_group_5"
</code></pre>

<p>i have up to 40 ad groups.</p>

<p>my code:</p>

<pre><code>columns=['date','converted','clicked','ad']
df2=pd.DataFrame(df,columns=columns)
df2.set_index(df2.date,inplace=True)
</code></pre>

<p>here i take only 1 column ('clicked') to plot against 'date'</p>

<pre><code>plt.figure()
df2.loc[:,['clicked','ad']].plot(kind='bar')
plt.show()
#df2.loc[:,['clicked','ad']].plot(kind='bar',colormap='ad')
</code></pre>

<p><a href="http://i.stack.imgur.com/2sGwR.png" rel="nofollow"><img src="http://i.stack.imgur.com/2sGwR.png" alt="enter image description here"></a></p>

<p>The line of code with <code>colormap</code> doesnt work because i dont have a color list. 
Ive seen answers where the user manually created color mapping of categories to color. I have 40 over categories and cant do it manually. </p>

<p>Is there any way to just set distinct value of 'ad' column to be automatically assigned a color?</p>

<p>Looking for something like this:
<a href="http://i.stack.imgur.com/EJezC.png" rel="nofollow"><img src="http://i.stack.imgur.com/EJezC.png" alt="enter image description here"></a></p>

<p>You could get distinct colours for each group present in the <code>ad</code> column as shown:</p>

<pre><code>df.set_index(['date'],inplace=True) 

# Empty list to append later
grouped_list = []
label_list = []

# Iterating through each group key grouped by ad
for label, key in df.groupby(['ad'])['clicked']:
    grouped_list.append(key)
    label_list.append(label)

# Concatenating the grouped list column-wise and filling Nans with 0's 
df_grouped_bar = pd.concat(grouped_list, axis=1).fillna(0)

# Renaming columns created to take on new names
df_grouped_bar.columns = label_list

# Bar plot with a chosen colormap
ax = df_grouped_bar.plot(kind='bar', stacked=True, figsize=(6,6), 
                         width=0.2, ylim=(0,5000), cmap=plt.cm.rainbow)
# Figure Aesthetics
ax.set_xticklabels(df_grouped_bar.index.format())
plt.ylabel('clicked')
plt.tight_layout()
plt.show()
</code></pre>

<p><a href="http://i.stack.imgur.com/KwdYe.png" rel="nofollow"><img src="http://i.stack.imgur.com/KwdYe.png" alt="Image"></a></p>

python pandas matplotlib