Python 柱状图加数据标签
文章目录
- 导入数据
- 计算对整体的比例
- 柱状图
导入数据
import pandas as pd
import numpy as np
topic = pd.read_csv('https://cocl.us/datascience_survey_data',index_col=0)
topic
计算对整体的比例
共有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
柱状图
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 柱状图加数据标签相关推荐
- 做柱状图加数据标签_Origin绘图:如何优雅的绘制堆叠柱状图
Origin绘图:如何优雅的绘制堆叠柱状图 柱状图用来比较两个或以上的价值(不同时间或者不同条件),只有一个变量,通常利用于较小的数据集分析.长条图亦可横向排列,或用多维方式表达.实际应用中,最简单的 ...
- Python使用matplotlib可视化柱状图、坐标轴标签的符号(-)显示为了方框□□、设置rcParams参数配置解决
Python使用matplotlib可视化柱状图.坐标轴标签的符号(-)显示为了方框□□.设置rcParams参数配置解决 目录
- python根据文件名打标签_使用python将图片按标签分入不同文件夹的方法
给定图像集如下,所有类别的图片均在一个文件夹内: 给定与图片名相匹配的表格,声明每张图片对应的类别(共有20个类别): 那么,如何根据表格中所给的类别将图片分入对应的文件夹内呢?以我的情况为例,我想将 ...
- python 柱状图如何添加数字标签_matplotlib可视化之如何给图形添加数据标签?
当我们获取完数据之后,一般来说数据可视化呈现的最基础图形就是:柱状图.水平条形图.折线图等等,在python的matplotlib库中分别可用bar.barh.plot函数来构建它们,再使用xtick ...
- Python读取excel表格数据并绘制成柱状图 | 数据排序、柱状图颜色、标签乱码等问题通通能够解决!
hello大家好, 我是你们的可爱丸, 我们又见面啦! python的功能十分强大, 它不仅可以用来做爬虫, 还可以用来做数据分析哦! 那么今天我就带着大家 用python 分析表格数据,并绘制成柱状 ...
- python matplotlib柱状图显示数据标签及百分号
简单方法,一行代码解决: import matplotlib.pyplot as plt y_data = [ 10, 21, 13, 7, ] x_data = ('a', 'b', 'c', 'd ...
- 柱状图添加数字标签_Python之利用Plotnine作图(一)——简单柱状图
NO.252 2020.01.05 工欲善其事,必先利其器 // - 前言 - ◆ ◆ ◆ ◆ 今天,小咖带大家利用Python的plotnine制作简单柱状图. R语言有一个强大的绘图工具ggplo ...
- python 柱状图 居中_python matplotlib模块: bar(柱状图)
plt模块的bar方法可以帮助我们绘制竖着的柱状图. 功能非常强大, 使用起来非常简单, 详细注释放在源码. 其中各种颜色的hex值可以从: 各种颜色hex值获取 源码: # coding=utf-8 ...
- python 柱状图 间距_专题第18篇:Python 绘图入门
我的施工之路 1我的施工计划2数字专题3字符串专题4列表专题5流程控制专题6编程风格专题7函数使用8面向对象编程(上篇)9面向对象编程(下篇)10十大数据结构11包和模块使用总结12Python正则专 ...
最新文章
- ArcEngine对Blob字段赋值的方法
- java筑基期(10)----ajaxjson(2)
- HTML 5.2 新特性介绍
- nofollow标签_如何Nofollow外链
- php判断全是中文正则,php判断是否为中文正则表达式大全
- python modbus类封装_Python | 面向对象程序设计来了!
- synchronized原理_面试必备—Synchronized 关键字使用、底层原理
- jetson nano 相关设置(开机自动登录、取消休眠和屏保、开机自启动程序)
- GIMP用Path作画了解一下
- xp虚拟服务器设置,VMWare的XP虚拟机网络设置
- Spring同mybatis整合讲义(事物)
- 设备和驱动器中删除空白图标
- 2021李宏毅机器学习笔记
- Qt调用Com组件--QT调用COM组件DLL(dumpCPP工具)
- 案例|工业物联网解决方案•空调系统智能监控运维云平台
- OpenCV 之 角点检测
- 跟着团子学SAP CS:SAP CS(客户服务)模块概览
- 域名显示服务器错误,域名解析出现错误是怎么回事?
- 人才引进--我搭上了顺风车
- Hie with the Pie(旅行商问题)