如何在python中显示条形图

2021-01-08 23点热度 0人点赞 0条评论

我正在尝试用python创建一个多条条形图。条形图应在每个条形图的顶部显示值。我有如下数据集:

Speciality                  Very interested Somewhat_interested Notinterested
Big Data (Spark/Hadoop)         1332           729                      127
Data Analysis / Statistics      1688           444                      60     
Data Journalism                 429            1081                     610

我尝试了以下代码:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

pd_dataframe = pd.read_csv('Test-Barchart.csv')
no_of_xaxis = pd_dataframe.Speciality.nunique()
ind = np.arange(no_of_xaxis)
xcord = pd_dataframe['Speciality'].tolist()

veryinterestedlist = pd_dataframe['Very interested'].tolist()
somewhatlist = pd_dataframe['Somewhat interested'].tolist()
notinterestedlist = pd_dataframe['Not interested'].tolist()

fig=plt.figure()
ax = fig.add_subplot(111)
width=0.8

rects1 = ax.bar(ind, veryinterestedlist, width, color='r')
rects2 = ax.bar(ind, somewhatlist, width, color='g')
rects3 = ax.bar(ind+width*2, notinterestedlist, width, color='b')

ax.legend( (rects1[0], rects2[0], rects3[0]), ('Very Interested', 
 'Somewhat Interested', 'Not Interested') )

def autolabel(rects):
   for rect in rects:
    h = rect.get_height()
    ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
            ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

ax.set_xticks(ind+width)
ax.set_xticklabels( xcord )
plt.show()

问题是, plt.show() 什么都没有表现出来!代码中没有任何错误。你能帮我解决这个问题吗?另外,如何将条形图颜色更改为十六进制代码颜色而不是R、G或B?例如#5bc0de

查看隐藏内容需要支付:¥1
查看

未经允许不得转载!如何在python中显示条形图

本文地址:https://ans.52learn.online/2141

ANS52LEARN

DO BEST