6

I have the following pandas dataframe:

              a   b
    bob      23  25
    john     13  21
    paul     20  19
    david    17  14
    michael  14  24
    neil     22  11 

    df.plot(kind='barh')

I used the pandas plot function. I want to make a barh chart with all the rows(names) having a different colors is there a way to do this?

enter image description here I need all person to have different colored bars.

michAmir
  • 375
  • 2
  • 4
  • 12
  • 1
    Thank you for including a simple example. It would be even better if we could copy and paste the code to generate the DataFrame. You should also include the code line you used to create the bar chart. – andrew May 25 '16 at 20:38
  • Thanks. Ive added the code to the code block. – michAmir May 25 '16 at 20:44

1 Answers1

9

It appears that Pandas only supports using the colormap attribute, which applies the same map to each row in your chart, e.g.:

df.plot(kind='barh', colormap='RdBu')

For your purposes, you need to use Matplotlib directly.

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a':[23, 13, 20, 17, 14, 22],
                   'b':[25, 21, 19, 14, 23, 11]},
                   index=['bob', 'john', 'paul', 'david', 'michael', 'neil'])

a_vals = df.a
b_vals = df.b
ind = np.arange(df.shape[0])
width = 0.35

# Set the colors
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'g']


def autolabel(bars):
    # attach some text labels
    for bar in bars:
        width = bar.get_width()
        ax.text(width*0.95, bar.get_y() + bar.get_height()/2,
                '%d' % int(width),
                ha='right', va='center')

# make the plots
fig, ax = plt.subplots()
a = ax.barh(ind, a_vals, width, color = colors) # plot a vals
b = ax.barh(ind + width, b_vals, width, color = colors, alpha=0.5)  # plot b vals
ax.set_yticks(ind + width)  # position axis ticks
ax.set_yticklabels(df.index)  # set them to the names
ax.legend((a[0], b[0]), ['a', 'b'], loc='center right')

autolabel(a)
autolabel(b)

plt.show()

Please refer to the following examples:

1 - matplotlib bar charts

2- changing individual colors on bar chart

enter image description here

Community
  • 1
  • 1
andrew
  • 3,929
  • 1
  • 25
  • 38
  • Thank you andrew. The output is exactly what im looking for. However, I get an AttributeError saying 'DataFrame' object has no attribute 'shap'. – michAmir May 25 '16 at 21:25
  • 1
    I changed shap to shape and it works. Thanks a lot!! – michAmir May 25 '16 at 21:30
  • @michAmir woops. Glad you caught it. I fixed it in the example. – andrew May 25 '16 at 21:53
  • sorry to bother you again. Im trying to show the value on the right from each bar. But im struggling with it. Can you help me out again? – michAmir May 26 '16 at 13:52
  • You can easily do this by reverse engineering the `autolablel()` from the "Matlab bar charts" link I posted above. Look at how I changed the version I posted in my code above. Just a matter of rotating the context by 90 degrees. – andrew May 26 '16 at 16:00