Python Matplotlib条形图,具有不同颜色的条形图和显示值的条形图
我想改变这段代码中条的颜色,所有条都是相同的颜色,我想在每个条的顶部显示不同的值,这些值存储在maxteet,p,n变量中Python Matplotlib条形图,具有不同颜色的条形图和显示值的条形图,python,python-2.7,matplotlib,bar-chart,data-visualization,Python,Python 2.7,Matplotlib,Bar Chart,Data Visualization,我想改变这段代码中条的颜色,所有条都是相同的颜色,我想在每个条的顶部显示不同的值,这些值存储在maxteet,p,n变量中 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_
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()
输出:
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()