【问题标题】:Matplotlib Bar chart with different color bars and bar showing valuesMatplotlib 条形图,带有不同的颜色条和显示值的条形图
【发布时间】:2017-05-13 05:05:03
【问题描述】:

我想更改此代码中条形的颜色,所有条形的颜色都相同,并且我想在每个条形的顶部显示不同的值,这些值存储在 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()

【问题讨论】:

    标签: python python-2.7 matplotlib bar-chart data-visualization


    【解决方案1】:
    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()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2017-06-04
      • 1970-01-01
      • 2020-10-07
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多