Python matplotlib怎么实现饼图、柱状图

发布时间:2022-02-19 16:32:59 作者:iii
来源:亿速云 阅读:183

这篇文章主要介绍“Python matplotlib怎么实现饼图、柱状图”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python matplotlib怎么实现饼图、柱状图”文章能帮助大家解决问题。

1、饼图

使用 pie() 方法绘制饼图: 

import matplotlib.pyplot as plt

print('\n-----欢迎来到yisu.com')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

labels = ['Strawberry', 'Apple', 'Banana', 'Pear', 'Orange']

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() 方法参数:

2、柱状图

使用bar()方法绘制柱状图:

import matplotlib.pyplot as plt

import numpy as np

print('\n-----欢迎来到yisu.com')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

fig, ax = plt.subplots()

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

weight = (100,135,50,83,92,66)

ax.bar(fruit, weight, align='center',width=0.7)

ax.set_ylabel('Weight')#设置x轴标签

ax.set_title('Histogram')

fig.savefig('matplot-bar.jpg')

plt.show()

bar()参数:

3、水平柱状图

使用barh()方法绘制水平柱状图:

import matplotlib.pyplot as plt

import numpy as np

print('\n-----欢迎来到yisu.com')   

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

fig, ax = plt.subplots()

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

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('weight')#设置x轴标签

ax.set_title('Horizontal bar chart')

fig.savefig('matplot-hor-bar.jpg')

plt.show()

barh() 方法参数:

ax.invert_yaxis()表示将y坐标反转,这样更符合阅读习惯,第0个元素在最上方显示。

4、分组柱状图

分组柱状图就是柱状图的组合形式,实际是2个柱状图合并在一起显示:

import matplotlib.pyplot as plt

import numpy as np

print('\n-----欢迎来到yisu.com')  

plt.rc('font',family='Arial',size='9')

plt.rc('axes',unicode_minus='False')

fruit = ('Banana', 'Strawberry', 'Watermelon', 'Apple', 'Papaya', 'Tomatoes')

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='weight')

ax.bar(x+width/2, count,  width=width,label='number')   

ax.set_title('Grouping histogram')

ax.set_ylabel('Weight / number')#设置y轴标签

ax.set_xticks(x)          #设置x轴坐标值

ax.set_xticklabels(fruit) #设置x轴坐标标签

fig.savefig('matplot-bar-group.jpg')

ax.legend()               #显示图例

plt.show()

关于“Python matplotlib怎么实现饼图、柱状图”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. Python数据可视化教程之Matplotlib实现各种图表实例
  2. Python干货:分享Python绘制六种可视化图表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python matplotlib

上一篇:ES6中的Map与Set怎么用

下一篇:Python如何实现Excel表格处理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》