2016-12-28 11 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

関連する問題