2015-05-01 3 views
2

Я создал следующую гистограмму с помощью pylabУкажите тип цвета для гистограммы pylab

enter image description here

Для каждой гистограммы есть способ, чтобы определить цвет? Ниже приведен код, который у меня есть

import numpy as np 
import matplotlib.pyplot as plt 


month = ["dec-09", "jan", "feb"] 
n = len(month) 

air = np.array([383.909, 395.913, 411.714]) 

ind = np.arange(n) 
width = 0.35 

print(n) 
print(ind) 

plt.bar(ind, air, width, color="yellow") 

plt.ylabel("KWH") 
plt.title("winter") 
plt.xticks(ind+width/2, ("dec-09", "jan", "feb")) 
plt.show() 

ответ

0

Да. Для plt.bar строка документации говорит:

Make a bar plot. 

Make a bar plot with rectangles bounded by: 

    `left`, `left` + `width`, `bottom`, `bottom` + `height` 
     (left, right, bottom and top edges) 

Parameters 
---------- 
left : sequence of scalars 
    the x coordinates of the left sides of the bars 

[snip] 

color : scalar or array-like, optional 
    the colors of the bar faces 

Таким образом, вы можете передать список цветов plt.bar и окрашивает каждый бар отдельно.

Так что ваш пример будет выглядеть так:

import numpy as np 
import matplotlib.pyplot as plt 

month = ["dec-09", "jan", "feb"] 
n = len(month) 

air = np.array([383.909, 395.913, 411.714]) 

ind = np.arange(n) 
width = 0.35 

fig, ax = plt.subplots() 
ax.bar(ind, air, width, color=["yellow", 'cornflowerblue', 'darkgreen']) 

ax.set_ylabel("KWH") 
ax.set_title("winter") 
ax.set_xticks(ind+width/2) 
ax.set_xticklabels(("dec-09", "jan", "feb")) 

enter image description here

Смежные вопросы