python条形图y轴_python 中条形图绘制
一、条形图绘制参数详解
1、bar(left, height, width=0.8, bottom=None, color=None, edgecolor=None, linewidth=None, tick_label=None, xerr=None, yerr=None, label = None, ecolor=None, align, log=False, **kwargs)
x:传递数值序列,指定条形图中x轴上的刻度值
height:传递数值序列,指定条形图y轴上的高度
width:指定条形图的宽度,默认为0.8
bottom:用于绘制堆叠条形图
color:指定条形图的填充色
edgecolor:指定条形图的边框色
linewidth:指定条形图边框的宽度,如果指定为0,表示不绘制边框
tick_label:指定条形图的刻度标签
xerr:如果参数不为None,表示在条形图的基础上添加误差棒
yerr:参数含义同xerr
label:指定条形图的标签,一般用以添加图例
ecolor:指定条形图误差棒的颜色align:指定x轴刻度标签的对齐方式,默认为center,表示刻度标签居中对齐,如果设置为edge,则表示在每个条形的左下角呈现刻度标签
log:bool类型参数,是否对坐标轴进行log变换,默认为False
**kwargs:关键字参数,用于对条形图进行其他设置,如透明度等
1 #条形图的绘制--垂直条形图
2 #读入数据
3 GDP = pd.read_excel('Province GDP 2017.xlsx')4 '''
5 Province GDP6 北京 2.87 上海 3.018 广东 8.999 江苏 8.5910 重庆 1.9511 天津 1.8612 '''
13 #设置绘图风格(不妨使用R语言中的ggplot2风格)
14 plt.style.use('ggplot')15 #绘制条形图
16 plt.bar(x = range(GDP.shape[0]), #指定条形图x轴的刻度值
17 height = GDP.GDP, #指定条形图y轴的数值
18 tick_label = GDP.Province, #指定条形图x轴的刻度标签
19 color = 'steelblue', #指定条形图的填充色
20 width = 0.8
21 )22 #添加y轴的标签
23 plt.ylabel('GDP(万亿)')24 #添加条形图的标题
25 plt.title('2017年度6个省份GDP分布')26 #为每个条形图添加数值标签
27 for x,y inenumerate(GDP.GDP):28 plt.text(x,y+0.1,'%s' %round(y,1),ha='center')29 #显示图形
30 plt.show()
1 #条形图的绘制--水平条形图
2 #对读入的数据作升序排序
3 GDP.sort_values(by = 'GDP', inplace =True)4 #绘制条形图
5 plt.barh(y = range(GDP.shape[0]), #指定条形图y轴的刻度值
6 width = GDP.GDP, #指定条形图x轴的数值
7 tick_label = GDP.Province, #指定条形图y轴的刻度标签
8 color = 'steelblue', #指定条形图的填充色
9 )10 #添加x轴的标签
11 plt.xlabel('GDP(万亿)')12 #添加条形图的标题
13 plt.title('2017年度6个省份GDP分布')14 #为每个条形图添加数值标签
15 for y,x inenumerate(GDP.GDP):16 plt.text(x+0.1,y,'%s' %round(x,1),va='center')17 #显示图形
18 plt.show()
③绘制堆叠条形图
1 importpandas as pd2 importmatplotlib.pyplot as plt3 #条形图的绘制--堆叠条形图
4 #读入数据
5 Industry_GDP = pd.read_excel('Industry_GDP.xlsx')6 #取出四个不同的季度标签,用作堆叠条形图x轴的刻度标签
7 Quarters =Industry_GDP.Quarter.unique()8 #取出第一产业的四季度值
9 Industry1 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第一产业']10 #重新设置行索引
11 Industry1.index =range(len(Quarters))12 #取出第二产业的四季度值
13 Industry2 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第二产业']14 #重新设置行索引
15 Industry2.index =range(len(Quarters))16 #取出第三产业的四季度值
17 Industry3 = Industry_GDP.GPD[Industry_GDP.Industry_Type == '第三产业']18
19 #绘制堆叠条形图
20 #中文乱码和坐标轴负号的处理
21 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']22 plt.rcParams['axes.unicode_minus'] =False23 #各季度下第一产业的条形图
24 plt.bar(x = range(len(Quarters)), height=Industry1, color = 'steelblue', label = '第一产业', tick_label =Quarters)25 #各季度下第二产业的条形图
26 plt.bar(x = range(len(Quarters)), height=Industry2, bottom = Industry1, color = 'green', label = '第二产业')27 #各季度下第三产业的条形图
28 plt.bar(x = range(len(Quarters)), height=Industry3, bottom = Industry1 + Industry2, color = 'red', label = '第三产业')29 #添加y轴标签
30 plt.ylabel('生成总值(亿)')31 #添加图形标题
32 plt.title('2017年各季度三产业总值')33 #显示各产业的图例
34 plt.legend(loc =2,fontsize = 'small')35 #显示图形
36 plt.show()
④水平交错条形图
1 #条形图的绘制--水平交错条形图
2 #导入第三方模块
3 importmatplotlib.pyplot as plt4 importnumpy as np5 importpandas as pd6 #读入数据
7 HuRun = pd.read_excel('HuRun.xlsx')8 #取出城市名称
9 Cities =HuRun.City.unique()10 #取出2016年各城市亿万资产家庭数
11 Counts2016 = HuRun.Counts[HuRun.Year == 2016]12 #取出2017年各城市亿万资产家庭数
13 Counts2017 = HuRun.Counts[HuRun.Year == 2017]14
15 #绘制水平交错条形图
16 bar_width = 0.4
17 plt.bar(x = np.arange(len(Cities)), height = Counts2016, label = '2016', color = 'steelblue', width =bar_width)18 plt.bar(x = np.arange(len(Cities))+bar_width, height = Counts2017, label = '2017', color = 'indianred', width =bar_width)19 #添加刻度标签(向右偏移0.225)
20 plt.xticks(np.arange(5)+0.2, Cities)21 #添加y轴标签
22 plt.ylabel('亿万资产家庭数')23 #添加图形标题
24 plt.title('近两年5个城市亿万资产家庭数比较')25 #添加图例
26 plt.legend()27 #显示图形
28 plt.show()
二、
①
1 #Pandas模块之垂直或水平条形图
2 #读入数据
3 GDP = pd.read_excel('Province GDP 2017.xlsx')4 #绘图(此时的数据集在前文已经按各省GDP做过升序处理)
5 GDP.GDP.plot(kind = 'bar', width = 0.8, rot = 0, color = 'steelblue', title = '2017年度6个省份GDP分布')6 #添加y轴标签
7 plt.ylabel('GDP(万亿)')8 #添加x轴刻度标签
9 plt.xticks(range(len(GDP.Province)), #指定刻度标签的位置
10 GDP.Province #指出具体的刻度标签值
11 )12 #为每个条形图添加数值标签
13 for x,y inenumerate(GDP.GDP):14 plt.text(x-0.1,y+0.2,'%s' %round(y,1),va='center')15 #显示图形
16 plt.show()
②
1 #Pandas模块之水平交错条形图
2 HuRun = pd.read_excel('HuRun.xlsx')3 HuRun_reshape = HuRun.pivot_table(index = 'City', columns='Year', values='Counts').reset_index()4 #对数据集降序排序
5 HuRun_reshape.sort_values(by = 2016, ascending = False, inplace =True)6 HuRun_reshape.plot(x = 'City', y = [2016,2017], kind = 'bar', color = ['steelblue', 'indianred'],7 rot = 0, #用于旋转x轴刻度标签的角度,0表示水平显示刻度标签
8 width = 0.8, title = '近两年5个城市亿万资产家庭数比较')9 #添加y轴标签
10 plt.ylabel('亿万资产家庭数')11 plt.xlabel('')12 plt.show()
③
1 #seaborn模块之垂直或水平条形图
2 #导入第三方模块
3 importseaborn as sns4
5 #读入数据
6 GDP = pd.read_excel('Province GDP 2017.xlsx')7 sns.barplot(y = 'Province', #指定条形图x轴的数据
8 x = 'GDP', #指定条形图y轴的数据
9 data = GDP, #指定需要绘图的数据集
10 color = 'steelblue', #指定条形图的填充色
11 orient = 'horizontal' #将条形图水平显示
12 )13 #重新设置x轴和y轴的标签
14 plt.xlabel('GDP(万亿)')15 plt.ylabel('')16 #添加图形的标题
17 plt.title('2017年度6个省份GDP分布')18 #为每个条形图添加数值标签
19 for y,x inenumerate(GDP.GDP):20 plt.text(x,y,'%s' %round(x,1),va='center')21 #显示图形
22 plt.show()
④
1 #读入数据
2 Titanic = pd.read_csv('titanic_train.csv')3 #绘制水平交错条形图
4 sns.barplot(x = 'Pclass', #指定x轴数据
5 y = 'Age', #指定y轴数据
6 hue = 'Sex', #指定分组数据
7 data = Titanic, #指定绘图数据集
8 palette = 'RdBu', #指定男女性别的不同颜色
9 errcolor = 'blue', #指定误差棒的颜色
10 errwidth=2, #指定误差棒的线宽
11 saturation = 1, #指定颜色的透明度,这里设置为无透明度
12 capsize = 0.05 #指定误差棒两端线条的宽度
13 )14 #添加图形标题
15 plt.title('各船舱等级中男女乘客的年龄差异')16 #显示图形
17 plt.show()
python条形图y轴_python 中条形图绘制相关推荐
- python画xy轴_python中的坐标轴该如何画?好画吗?
曾经的数学爱搭不理,结果在工作中发现需要用python去解决相关的问题.数学不好的小伙伴已经开始发愁了.不要着急,小编跟你们一起想办法.为了方便python小白的理解,我们还是从最基础的入手.基础知识 ...
- python修改y轴刻度_Python | Y轴刻度限制
python修改y轴刻度 In some cases, we need to visualize our data within some defined range rather than the ...
- 【计算机图形学】中点画线法实现焦点在x、y轴上的椭圆绘制
[计算机图形学]中点画线法实现焦点在x.y轴上的椭圆绘制 一.中点画线法原理简介 1.建立基础 中点画线法的原理介绍见直线绘制的博文中点画线法实现任意斜率直线的绘制.基本思路是以下一点在椭圆外/内的位 ...
- python算法和数据结构_Python中的数据结构和算法
python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...
- 双y轴图中怎么设置中文字体为仿宋_GB2312,英文字体为Times New Roman
如果您使用的是 Matplotlib 库,可以使用以下代码设置双 y 轴图中的字体: import matplotlib.pyplot as pltfrom matplotlib.font_manag ...
- python 三维数据绘图_Python中三维坐标空间绘制的实现
在三维空间绘制点,线,面 1.绘制点 用scatter()散点绘制三维坐标点from matplotlib import pyplot as plt from mpl_toolkits.mplot3d ...
- python画三维坐标_Python中三维坐标空间绘制的实现
在三维空间绘制点,线,面 1.绘制点 用scatter()散点绘制三维坐标点 from matplotlib import pyplot as plt from mpl_toolkits.mplot3 ...
- python双y轴的折线图_python matplotlib实现双Y轴的实例
python matplotlib实现双Y轴的实例 如下所示: import matplotlib.pyplot as plt import numpy as np x = np.arange(0., ...
- python画图y轴在右侧_解决python中画图时x,y轴名称出现中文乱码的问题
如下所示: #-*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from matplotlib.font_ ...
- python画xy轴_python画双y轴图像的示例代码
很多时候可能需要在一个图中画出多条函数图像,但是可能y轴的物理含义不一样,或是数值范围相差较大,此时就需要双y轴. matplotlib和seaborn都可以画双y轴图像. 一个例子: import ...
最新文章
- 共享程序集和强命名程序集(3):强命名程序集的一些作用
- ADO.NET编程小错误
- js中用到的正则表达式
- MySQL触发器使用详解
- 新浪微博,请砍掉90%的功能
- C++11 unique_ptr用法
- [Unity3D]Unity3D游戏开发Lua随着游戏的债券(于)
- html.parser python_python模块之HTMLParser
- 擦地机器人毕业设计_救援机器人毕业设计
- mac上启用tftp服务器
- Centos7下编译安装Nginx、Mysql、PHP(文章底部包含一键安装脚本)
- Windows11 + Linux子系统(ubuntu)体验(篇一)
- Luarocks的使用
- IDEA打包jar包并运行
- Markdown 图片居中并添加标题
- for循环占用的指令周期问题
- Java反射机制实现与原理
- 【GDOI2017模拟8.14】守鹤之砂
- GitHub获百万下载的阿里P5-P9必刷知识体系图核心手册
- 《2021大数据产业年度创新服务企业》榜重磅发布丨金猿奖