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

관련 문제