24

Is it possible to change only the color of a single bar in a graph made by matplotlib?

alt text

Let's say I've evaluation 1 to 10 and for each one I've a graph generate when the user choice the evaluation. For each evaluation one of this boys will win.

So for each graph, I would like to leave the winner bar in a different color, let's say Jim won evaluation1. Jim bar would be red, and the others blue.

I have a dictionary with the values, what I tried to do was something like this:

for value in dictionary.keys(): # keys are the names of the boys
    if winner == value:
        facecolor = 'red'
    else:
        facecolor = 'blue'

ax.bar(ind, num, width, facecolor=facecolor)

Anyone knows a way of doing this?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
psoares
  • 4,733
  • 7
  • 41
  • 55

2 Answers2

39

You need to use color instead of facecolor. You can also specify color as a list instead of a scalar value. So for your example, you could have color=['r','b','b','b','b']

For example,

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

N = 5
ind = np.arange(N)
width = 0.5
vals = [1,2,3,4,5]
colors = ['r','b','b','b','b']
ax.barh(ind, vals, width, color=colors)

plt.show()

is a full example showing you what you want.

To answer your comment:

colors = []
for value in dictionary.keys(): # keys are the names of the boys
    if winner == value:
        colors.append('r')
    else:
        colors.append('b')

bar(ind,num,width,color=colors)
GWW
  • 43,129
  • 11
  • 115
  • 108
  • Actually is you use facecolor in a bar chart it also work. But anyway I tried changing to color and it's not that the issue – psoares Sep 30 '10 at 17:07
  • Ah okay, I've never used facecolor before, my mistake. But you can use color as a list, I'm not sure if facecolor works the same way. – GWW Sep 30 '10 at 17:09
  • That will be a nice solution but the problem is the red bar may change. In the example I use the boys, so the winner may be John and other time Will. I do the graph automatically, so if you choice evaluation1, for instance winner is Peter but in evaluation 8 the winner may be Simon. – psoares Sep 30 '10 at 17:25
  • When you do your evaluation loop through the values to generate the color array. I put an example in my answer. – GWW Sep 30 '10 at 17:27
  • What if I wanted to call plot instead of bar? – Wok Nov 03 '10 at 17:16
17

for seaborn you can do something like this:

import seaborn as sns
import numpy as np

values = np.array([2,5,3,6,4,7,1])   
idx = np.array(list('abcdefg')) 
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]
sns.barplot(x=idx, y=values, palette=clrs) # color=clrs)

enter image description here

for matplotlib:

import numpy as np
import matplotlib.pyplot as plt

values = np.array([2,5,3,6,4,7,1])   
idx = np.array(list('abcdefg')) 
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]
plt.bar(idx, values, color=clrs, width=0.4)
plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Vadim
  • 4,219
  • 1
  • 29
  • 44