-
Python数据可视化:折线图、柱状图、饼图代码
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
作者:godweiyang
来源:算法码上来
Python爬虫、数据分析、网站开发等案例教程视频免费在线观看
https://www.xin3721.com/eschool/pythonxin3721/
有很多很牛b的作图教程,我也学不来,就扔给大家自己学吧:
折线图
代码
import numpy as np import 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 np import 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 np import 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()
效果

栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数