1

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.

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()
halfer
  • 19,824
  • 17
  • 99
  • 186
IFfy KhAn
  • 71
  • 2
  • 7

1 Answers1

3
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:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241