-
Matplotlib 饼图
2019-10-28 12:21:26章节Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 ... Matplotlib 饼图 Matplotlib 直方图 Matplotlib 散点图 Matplotlib 填充图 Matplotlib 网格 Matplot...
章节
饼图是另一种常见的图形类型,可以使用
pie()
方法制作饼图。示例
# 导入numpy库与matplotlib.pyplot库 import numpy as np import matplotlib.pyplot as plt # 准备数据:公司市场占有率 firms = ["Firm A", "Firm B", "Firm C", "Firm D", "Firm E"] market_share = [20, 25, 15, 10, 20] # 设置第二项为爆炸(散开)状态 Explode = [0, 0.1, 0, 0, 0] # 绘制图形 plt.pie(market_share, explode=Explode, labels=firms, shadow=True, startangle=45) plt.axis('equal') plt.legend(title="List of Firms") # 显示 plt.show()
输出
-
matplotlib饼图
2019-01-20 17:47:45 -
python+matplotlib 饼图
2019-06-30 15:32:08python+matplotlib 饼图https://blog.csdn.net/crisschan/article/details/71107010python+matplotlib 饼图https://blog.csdn.net/crisschan/article/details/71107010
-
matplotlib 饼图 dataframe_数据可视化~matplotlib饼图、柱状图
2021-01-24 04:18:28matplotlib除了可以绘制曲线图, 还可以绘制统计图形,这篇文章介绍饼图、柱状图等几种常用统计图形的绘制方法。1、饼图使用pie()方法绘制饼图:print('\n-----欢迎来到juzicode.com')print('-----公众号: 桔子code/...matplotlib除了可以绘制曲线图, 还可以绘制统计图形,这篇文章介绍饼图、柱状图等几种常用统计图形的绘制方法。
1、饼图
使用pie()方法绘制饼图:
print('\n-----欢迎来到juzicode.com')print('-----公众号: 桔子code/juzicode \n') import matplotlib.pyplot as pltif __name__ == '__main__': plt.rc('font',family='Youyuan',size='9') plt.rc('axes',unicode_minus='False') labels = ['桔子', '苹果', '香蕉', '梨子', '橙子'] sizes = [39, 20, 55, 30,25] # 每个元素的值,会自动根据该值计算百分比 explode = [0.1, 0.2, 0, 0, 0] # 每个元素的膨胀距离,这里指定了第0和第1个 fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=0) # autopct 精度 startangle 第1个元素的起始角位置,其他元素逆时针方向组织,shadow 是否使用阴影 ax.axis('scaled') #设置饼图的样式,设置为equals显示的会是圆形 fig.savefig('matplot-basic-pie.jpg') plt.show()
ax.pie() 方法参数:
sizes:各元素的绝对数值大小,相对百分比会根据这些值计算;
explode:各个部分向外弹出的值;
autopct:百分比的显示精度;
shadow:是否显示阴影;
startangle:起始元素的位置,就是表示labels[0]的起始角位置,剩下的元素会逆时针方向组织。
2、柱状图
使用bar()方法绘制柱状图:
import matplotlib.pyplot as pltimport numpy as npif __name__ == '__main__': plt.rc('font',family='Youyuan',size='9') plt.rc('axes',unicode_minus='False') fig, ax = plt.subplots() fruit = ('桔子', '橙子', '西瓜', '苹果', '香蕉', '西红柿') weight = (100,135,50,83,92,66) ax.bar(fruit, weight, align='center',width=0.7) ax.set_ylabel('重量')#设置x轴标签 ax.set_title('柱状图 (by桔子code)') fig.savefig('matplot-bar.jpg') plt.show()
bar()参数:
第1个固定参数:x坐标点名称
第2个固定参数:y坐标值
align:对齐方式;
width:柱宽度;
3、水平柱状图
使用barh()方法绘制水平柱状图:
import matplotlib.pyplot as pltimport numpy as npif __name__ == '__main__': plt.rc('font',family='Youyuan',size='9') plt.rc('axes',unicode_minus='False') fig, ax = plt.subplots() fruit = ('桔子', '橙子', '西瓜', '苹果', '香蕉','西红柿') y_pos = np.arange(len(fruit)) weight = (100,135,50,83,92,66) ax.barh(y_pos, weight, align='center',height=0.7) ax.set_yticks(y_pos)#设置y轴坐标 ax.set_yticklabels(fruit)#设置y轴标签 ax.invert_yaxis() # 设置标签从上到下,更符合阅读习惯 ax.set_xlabel('重量')#设置x轴标签 ax.set_title('水平柱状图 (by桔子code)') fig.savefig('matplot-hor-bar.jpg') plt.show()
barh() 方法参数:
第1个固定参数:y轴坐标;
第2个固定参数:宽度值,实际上对应的是x轴的长度;
align:对齐方法,可选center和edge,表示柱图的位置和对应y轴坐标的关系;
height:柱图y方向的高度
ax.invert_yaxis()表示将y坐标反转,这样更符合阅读习惯,第0个元素在最上方显示。
4、分组柱状图
分组柱状图就是柱状图的组合形式,实际是2个柱状图合并在一起显示:
import matplotlib.pyplot as pltimport numpy as npif __name__ == '__main__': plt.rc('font',family='Youyuan',size='9') plt.rc('axes',unicode_minus='False') fruit = ('桔子', '橙子', '西瓜', '苹果', '香蕉','西红柿') weight = (100,135,50,83,92,66) count = (20,15,30,53,22,36) x = np.arange(len(fruit)) fig, ax = plt.subplots() width = 0.4 ax.bar(x-width/2, weight, width=width,label='重量') ax.bar(x+width/2, count, width=width,label='数量') ax.set_title('分组柱状图 (by桔子code)') ax.set_ylabel('重量/数量')#设置y轴标签 ax.set_xticks(x) #设置x轴坐标值 ax.set_xticklabels(fruit) #设置x轴坐标标签 fig.savefig('matplot-bar-group.jpg') ax.legend() #显示图例 plt.show()
bar()入参的第1个参数不是直接使用分类形式的tuple,而是根据分类的数量重新定义的numpy数组,这样2个柱形图就可以根据该数值加减宽度的一半实现并排放置。
---End---
推荐阅读:
数据可视化~matplotlib显示多个子图
数据可视化~matplotlib基本绘图方法如何优雅地解决matplotlib不能正常显示中文和负号的问题
好冷的Python-- if __name__==’__main__’是啥东东
奇文共欣赏~UTF-8设计与源码实现
编码: 一个隐藏了30多年的bug,Windows含蓄说过某通不行?
关注微信公众号"桔子code",不错过更多精彩。
-
python matplotlib 饼图标签重叠_如何避免matplotlib饼图中的标签和autoct重叠?
2020-12-04 20:31:03或者,可以将图例放在饼图旁边:import matplotlib.pyplot as pltimport numpy as npx = np.char.array(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct', 'Nov','Dec'])y = np.array([234, 64, 54,10... -
python matplotlib 饼图标签重叠_如何避免标签重叠matplotlib饼图中的autopct?
2020-12-04 20:31:07解决方案 Alternatively you can put the legends beside the pie graph: import matplotlib.pyplot as plt import numpy as np x = np.char.array(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct',... -
matplotlib 饼图 dataframe_Matplotlib图鉴进阶饼图两张
2021-01-15 10:28:28大家好,从今天起,我们将开始更新Matplotlib进阶饼图图鉴。本文将讲解以下二个进阶的饼图绘制方法。进阶饼图-007下面我们就来讲解如何绘制第一个进阶饼图,注意,代码在以下环境全部通过测试:Python 3.7.1... -
matalb饼图matplotlib饼图
2020-04-14 15:17:56clear all; x=[1 5 0.5 3.5 2];...pie(x,explode)%绘制饼图 colormap jet pie3(x,explode) colormao hsv import matplotlib.pyplot as plt import numpy as np import math from mpl_toolkits.mplot... -
Matplotlib饼图注释
2021-01-07 10:22:33import matplotlib.pyplot as plt plt.figure(figsize=(9,9)) # 环形饼图元素 recipe = ["225g flour", "90g sugar", "1 egg", "60g butter", "100ml milk", "1/2package of yeast"] # ⽐例 data = [225, 90 -
matplotlib 饼图 参数
2020-12-23 21:53:48一、调整字体大小 #将三个返回值分别赋给三个变量 patches,l_text,p_text=plt.pie(x=tmp, labels =["high", "mid", "low"], autopct = '%0.1f%%', pctdistance = 0.5, shadow = T -
matplotlib 饼图 dataframe_马青公式求任意高精度π值和matplotlib统计饼图
2021-01-16 20:18:02-*-coding:utf-8 -*-#第1步:导入模块import matplotlib.pyplot as plt#第2步:数据统计和预处理#马青公式获得π的100位pi_100=['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', ... -
matplotlib饼图绘制奇招
2020-07-28 14:59:02饼图 啥都不说,先绘制最简单的饼图 plt.pie(x) x = [2,7,12] plt.pie(x) plt.show() 使用 np.info(plt.pie) 可以查看 plt.pie() 参数信息: pie(x, explode=None, labels=None, colors=None, autopct=None... -
matplotlib 饼图绘制
2017-12-08 13:50:11matplotlib.pyplot as plt # 设置绘图的主题风格(不妨使用R中的ggplot分隔) plt.style.use( 'ggplot' ) # 构造数据 edu = [ 0.2515 , 0.3724 , 0.3336 , 0.0368 , 0.0057 ] labels = [ '中专' , '大专' , ... -
matplotlib饼图的绘制
2019-09-11 22:22:13饼图的适用场景 统计部分与部分,以及部分与整体的关系 代码和参数如下 import numpy as np plt.rcParams['font.sans-serif']='SimHei' plt.rcParams['axes.unicode_minus']=False res = np.load(r'.......... -
pandas-matplotlib饼图
2018-12-12 17:42:25# -*-coding:utf-8 -*- ...import matplotlib.pyplot as plt students = pd.read_excel("C:/Users/asus/Downloads/pandas/012/Students.xlsx",index_col='From') print(students) students['2017... -
Matplotlib 饼图(Pie graph)
2021-01-13 10:28:18import matplotlib.pyplot as plt # 显示中文标签 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 饼图 Z = np.array([0.3, 0.3, 0.3, 0.1]) plt.pie(Z, labels=['... -
Matplotlib饼图与嵌套图
2018-09-24 23:15:10饼图pie import matplotlib.pyplot as plt import numpy as np %matplotlib inline m = 51212 f = 40732 m_perc = m/(m+f) f_perc = f/(m+f) colors = ['navy','lightcoral'] labels = ['Male','Female'] plt....