简体   繁体   English

Matplotlib条形图,带有不同的颜色条和显示值的条

[英]Matplotlib Bar chart with different color bars and bar showing values

I want to change the color of the bars in this code all the bars are of same color and I want to show different values on top of each bar which are stored in maxtweet,p,n variables. 我想在这段代码中更改条形的颜色,所有条形都是相同的颜色,并且我想在每个条形的顶部显示不同的值,这些值存储在maxtweet,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_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()

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM