Python怎么实现折线图、柱状图、饼图
发表于:2024-10-02 作者:千家信息网编辑
千家信息网最后更新 2024年10月02日,本篇内容介绍了"Python怎么实现折线图、柱状图、饼图"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2024年10月02日Python怎么实现折线图、柱状图、饼图
本篇内容介绍了"Python怎么实现折线图、柱状图、饼图"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
折线图
代码
import numpy as npimport matplotlib.pyplot as plt# x轴刻度标签x_ticks = ['a', 'b', 'c', 'd', 'e', 'f']# x轴范围(0, 1, ..., len(x_ticks)-1)x = np.arange(len(x_ticks))# 第1条折线数据y1 = [5, 3, 2, 4, 1, 6]# 第2条折线数据y2 = [3, 1, 6, 5, 2, 4]# 设置画布大小plt.figure(figsize=(10, 6))# 画第1条折线,参数看名字就懂,还可以自定义数据点样式等等。plt.plot(x, y1, color='#FF0000', label='label1', linewidth=3.0)# 画第2条折线plt.plot(x, y2, color='#00FF00', label='label2', linewidth=3.0)# 给第1条折线数据点加上数值,前两个参数是坐标,第三个是数值,ha和va分别是水平和垂直位置(数据点相对数值)。for a, b in zip(x, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 给第2条折线数据点加上数值for a, b in zip(x, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 画水平横线,参数分别表示在y=3,x=0~len(x)-1处画直线。plt.hlines(3, 0, len(x)-1, colors = "#000000", linestyles = "dashed")# 添加x轴和y轴刻度标签plt.xticks([r for r in x], x_ticks, fontsize=18, rotation=20)plt.yticks(fontsize=18)# 添加x轴和y轴标签plt.xlabel(u'x_label', fontsize=18)plt.ylabel(u'y_label', fontsize=18)# 标题plt.title(u'Title', fontsize=18)# 图例plt.legend(fontsize=18)# 保存图片plt.savefig('./figure.pdf', bbox_inches='tight')# 显示图片plt.show()
效果
柱状图
代码
import numpy as npimport matplotlib.pyplot as plt# x轴刻度标签x_ticks = ['a', 'b', 'c', 'd', 'e', 'f']# 柱的宽度barWidth = 0.25# 第1个柱的x轴范围(每个柱子的中点)(0, 1, ..., len(x_ticks)-1)x1 = np.arange(len(x_ticks))# 第2个柱的x轴范围(每个柱子的中点)x2 = [x + barWidth for x in x1]# 第1个柱数据y1 = [5, 3, 2, 4, 1, 6]# 第2个柱数据y2 = [3, 1, 6, 5, 2, 4]# 设置画布大小plt.figure(figsize=(10, 6))# 画第1个柱plt.bar(x1, y1, color='#FF0000', width=barWidth, label='label1')# 画第2个柱plt.bar(x2, y2, color='#00FF00', width=barWidth, label='label2')# 给第1个柱数据点加上数值,前两个参数是坐标,第三个是数值,ha和va分别是水平和垂直位置(数据点相对数值)。for a, b in zip(x1, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 给第2个柱数据点加上数值for a, b in zip(x2, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 画水平横线plt.hlines(3, 0, len(x_ticks)-1+barWidth, colors = "#000000", linestyles = "dashed")# 添加x轴和y轴刻度标签plt.xticks([r + barWidth/2 for r in x1], x_ticks, fontsize=18)plt.yticks(fontsize=18)# 添加x轴和y轴标签plt.xlabel(u'x_label', fontsize=18)plt.ylabel(u'y_label', fontsize=18)# 标题plt.title(u'Title', fontsize=18)# 图例plt.legend(fontsize=18)# 保存图片plt.savefig('./figure.pdf', bbox_inches='tight')# 显示图片plt.show()
效果
饼图
代码
import numpy as npimport matplotlib.pyplot as plt# 设置画布大小plt.figure(figsize=(10, 10))# 设置每块区域的标签labels = ['a', 'b', 'c', 'd', 'e']# 设置每块区域离圆心的距离,这里a区域凸出一点点explode = [0.05, 0.01, 0.01, 0.01, 0.01]# 设置每块区域的值values = [1, 5, 2, 4, 3]# 设置每块区域的颜色colors = ['#F5DEB3', '#87CEFA', '#FFB6C1', '#90EE90', '#D3D3D3']_, l_text, p_text = plt.pie(values, explode=explode, labels=labels, autopct='%1.1f%%', colors=colors)# 设置标签字体大小for t in l_text: t.set_size(18)# 设置数值字体大小for t in p_text: t.set_size(18)# 标题plt.title(u'Title', fontsize=18)# 图例plt.legend(fontsize=18)# 保存图片plt.savefig('./figure.pdf', bbox_inches='tight')# 显示图片plt.show()
"Python怎么实现折线图、柱状图、饼图"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
数据
数值
标签
图片
折线
区域
大小
刻度
参数
水平
线图
柱状
代码
图例
标题
画布
范围
三个
两个
中点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
职业院校劳务派遣软件开发
幼儿园小班网络安全教育图文
分布式数据库管理系统实践
电脑服务器最好的是什么
encap五星网络安全
杭州计算机软件开发大概要多少钱
正规手机网络安全公司
本科两年软件开发工资
创建数据库的全数据名
数据库批量建用户
网络技术员毕业顶岗实习
善林金融互联网金融科技委会
网络安全法对单位用户
第二章gis空间数据库资料
网络安全类通讯稿
安恒数据库审计默认
lol 登入服务器
肠外营养软件开发设计
怎么从打卡机里导出数据库
241数据库错误
国产网络视频服务器厂家
vs2010怎么查看数据库
软件开发企业上市
网闸是物理层面的网络安全隔离
软件开发用户使用手册
企业蔬菜配送软件开发
手机版空岛战争服务器推荐
江阴服务器维修企业
web服务器 多线程
网络安全正确密码