簡體   English   中英

Matplotlib條形圖,帶有不同的顏色條和顯示值的條

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

我想在這段代碼中更改條形的顏色,所有條形都是相同的顏色,並且我想在每個條形的頂部顯示不同的值,這些值存儲在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()

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM