Python 柱状图加数据标签

文章目录

      • 导入数据
      • 计算对整体的比例
      • 柱状图

导入数据

import pandas as pd
import numpy as np
topic = pd.read_csv('https://cocl.us/datascience_survey_data',index_col=0)
topic

Python 柱状图加数据标签_第1张图片

计算对整体的比例

共有2233人参加了调研

#Sort the dataframe in descending order of Very interested. 降序排列
topic.sort_values(['Very interested'],ascending=False,inplace=True)
#Convert the numbers into percentages of the total number of respondents.. Round percentages to 2 decimal places. 计算百分比,保留两位小数
topicp= (100*topic/2233).round(2)
topicp

Python 柱状图加数据标签_第2张图片

柱状图

fig = plt.figure()
#use a figure size of (20, 8),bar width of 0.8, 设置图片大小,柱宽
#use color #5cb85c for the Very interested bars, 设置柱子颜色
#color #5bc0de for the Somewhat interested bars,
#color #d9534f for the Not interested bars,
c=topicp.plot(kind='bar', y=['Very interested','Somewhat interested','Not interested'],figsize=(20, 8),width=0.8,
           color=['#5cb85c','#5bc0de','#d9534f'],fontsize=14)
#use font size 14 for the bar labels, percentages, and legend, 图例颜色
plt.legend(fontsize=14)
#use font size 16 for the title, and, 标题字号
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize=16)
plt.yticks([]) # y轴空轴
#display the percentages above the bars as shown above 数据标签列表
x=np.arange(len(topicp.index))
yv=np.array(list(topicp['Very interested']))
ys=np.array(list(topicp['Somewhat interested']))
yn=np.array(list(topicp['Not interested']))
for a,b in zip(x,yv): ##控制标签位置
    plt.text(a-0.27,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
for a,b in zip(x,ys):
    plt.text(a,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
for a,b in zip(x,yn):
    plt.text(a+0.27,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
#remove the left, top, and right borders. 去掉图片边框
c.spines['top'].set_visible(False)
c.spines['right'].set_visible(False)
#c.spines['bottom'].set_visible(False) 保留横坐标边框
c.spines['left'].set_visible(False)
plt.show

Python 柱状图加数据标签_第3张图片

你可能感兴趣的:(数据可视化)