2016-12-28 84 views
1

我想改變條的顏色在此代碼的所有條是相同顏色的,我想顯示被存儲在maxtweet在每個柱的頂部不同的值,p,n個變量。Matplotlib條形圖不同顏色的杆和杆表示值

x=[] 
x.append(max_tweets) 
x.append(p) 
x.append(n) 
label=('tweets','positivity','nagitivity') 
label_pos=np.arange(len(label)) 
plt.bar(label_pos,x,align='center',color='k') 
plt.xticks(label_pos,label) 
plt.xlabel('People Behaviour and Emotions') 
plt.title('Sentiment Analysis') 
plt.show() 

回答

2
import matplotlib.pylab as plt 
import numpy as np 
max_tweets = 19 
p = 20 
n = 30 

datas = [{'label':'tweets', 'color': 'r', 'height': max_tweets}, 
    {'label':'positivity', 'color': 'g', 'height': p}, 
    {'label':'nagitivity', 'color': 'b', 'height': n}] 

i = 0 
for data in datas: 
    plt.bar(i, data['height'],align='center',color=data['color']) 
    i += 1 

labels = [data['label'] for data in datas] 
pos = [i for i in range(len(datas)) ] 
plt.xticks(pos, labels) 
plt.xlabel('People Behaviour and Emotions') 
plt.title('Sentiment Analysis') 
plt.show() 

輸出:

enter image description here