python数据可视化的那些操作你了解吗
作者:橙橙小狸猫 时间:2023-11-04 15:18:55
0. 前言
数据处理过程中,可视化可以更直观得感受数据,因此打算结合自己的一些实践经理,以效果为准写这篇博客。内容应该会不断扩充。
1. matplotlib中figure、subplot和plot等什么关系
记住这几个关系可以结合实际。假设你去外面写生要带哪些工具呢,包括画板、画纸还有画笔,那么就可以一一对应了。
函数 | 工具 |
---|---|
figure | 画板 |
subplot、add_subplot | 画纸 |
plot、hist、scatter | 画笔 |
那么再往深处想,画纸贴在画板上,画纸可以裁剪成多块布局在画板上,而画笔只能画在纸上,可能这样讲有点笼统,下面一个代码配合注释就可以清晰明白啦。(感觉需要记住以下代码)
代码
import matplotlib.pyplot as plt
import numpy as np
# 拿起画板
fig = plt.figure()
# 在画板上贴上画纸
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
# 一步完成(直接拿起画板和画纸)-----------------
# ax1 = plt.subplot(221)
# ax2 = plt.subplot(222)
# ax3 = plt.subplot(223)
# ----------------------------------------
# 在画纸上作图
ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30))
ax3.plot(np.random.randn(50).cumsum(), 'k--')
plt.show()
运行结果
函数解析
代码行 | 作用 | 参考链接 |
---|---|---|
ax1.hist(np.random.randn(100), bins=20, color=‘k’, alpha=0.3) | 绘制直方图 | python用hist参数解读 |
2. 画图的细节修改
依次完成以下的画图效果:
1.一个正弦函数和一个随机数值的曲线,正弦函数直线,随机数值曲线虚线以及其他样式修改;
2.图例、标签等修改;
3.加上标注,标注范围内用红色矩形表示。
2.1 plot画图形式修改
代码
import matplotlib.pyplot as plt
import numpy as np
# 拿起画板
fig = plt.figure()
# 贴上画纸
ax1 = fig.add_subplot(111)
# 数据准备
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7个随机数
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 画图
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3)
ax1.plot(data_random, linestyle='dashed', color='b', marker='o')
plt.show()
运行结果
2.2 添加图例、标签等
代码
import matplotlib.pyplot as plt
import numpy as np
# 拿起画板
fig = plt.figure()
# 贴上画纸
ax1 = fig.add_subplot(111)
# 数据准备
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7个随机数
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 画图
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
#-----------------添加部分------------------
# 添加标题
ax1.set_title('Title')
# 添加x轴名称
ax1.set_xlabel('x')
# 设置x轴坐标范围
ax1.set_xlim(xmin=0, xmax=6)
# 添加图例,在plot处加上label
ax1.legend(loc='best')
#----------------------------------------
plt.show()
运行结果
2.3 在图上画注解和矩形
代码
import matplotlib.pyplot as plt
import numpy as np
# 拿起画板
fig = plt.figure()
# 贴上画纸
ax1 = fig.add_subplot(111)
# 数据准备
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7个随机数
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 画图
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# 添加标题
ax1.set_title('Title')
# 添加x轴名称
ax1.set_xlabel('x')
# 设置x轴坐标范围
ax1.set_xlim(xmin=0, xmax=6)
# 添加图例
ax1.legend(loc='best')
#-----------------添加部分------------------
# 注解
ax1.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax1.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax1.axis()[2]), np.pi, ax1.axis()[3] - ax1.axis()[2], color='r', alpha=0.3) # 起始坐标点,width, height
ax1.add_patch(rect)
#-----------------------------------------
plt.show()
运行结果
3. 图形保存
plt.savefig('figpath.png', dpi=400)
注意要放在show前面。
完整代码:
import matplotlib.pyplot as plt
import numpy as np
# 拿起画板
fig = plt.figure()
# 贴上画纸
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
# 数据准备
x_sin = np.arange(0, 6, 0.001) # [0, 6]
y_sin = np.sin(x_sin)
data_random = np.zeros(7) # 生成[-1,1]的7个随机数
for i in range(0, 6):
data_random[i] = np.random.uniform(-1, 1)
# 画图
ax1.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax1.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax2.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax2.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
ax3.plot(x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin')
ax3.plot(data_random, linestyle='dashed', color='b', marker='o', label='random')
# # 添加标题
ax2.set_title('Title')
# 添加x轴名称
ax2.set_xlabel('x')
# 设置x轴坐标范围
ax2.set_xlim(xmin=0, xmax=6)
# 添加图例
ax2.legend(loc='best')
ax3.set_title('Title')
# 添加x轴名称
ax3.set_xlabel('x')
# 设置x轴坐标范围
ax3.set_xlim(xmin=0, xmax=6)
# 添加图例
ax3.legend(loc='best')
# 注解
ax3.annotate('max', xy=((np.pi) / 2, np.sin(np.pi/2)),
xytext=((np.pi) / 2, np.sin(np.pi/2)-0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
ax3.annotate('min', xy=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)),
xytext=((np.pi) * 3 / 2, np.sin(np.pi * 3 / 2)+0.2),
arrowprops=dict(facecolor='black', headwidth=4, width=2,headlength=4),
horizontalalignment='left', verticalalignment='top')
# 矩形
# print(ax1.axis())
rect = plt.Rectangle((np.pi / 2, ax3.axis()[2]), np.pi, ax3.axis()[3] - ax3.axis()[2], color='r', alpha=0.3) # 起始坐标点,width, height
ax3.add_patch(rect)
#-----------------添加部分------------------
plt.savefig('figpath.png', dpi=400)
#------------------------------------------
plt.show()
来源:https://blog.csdn.net/weixin_42442319/article/details/122650968
标签:python,数据,可视化,操作
0
投稿
猜你喜欢
asp中Adodb.Stream 的使用说明
2007-09-11 13:53:00
SQL 2008的变更数据捕获——跟踪可变部分
2009-03-20 11:47:00
Python编程中闭包的变量作用域问题解析
2023-07-27 01:59:47
Go素数筛选分析详解
2023-07-22 11:50:02
asp.net DropDownList实现二级联动效果
2023-07-23 07:48:41
asp访google分页代码
2009-07-10 13:06:00
如何对PHP程序中的常见漏洞进行攻击(下)
2023-11-16 14:50:19
用户是如何浏览你的网站的
2010-05-03 14:26:00
为MySQL提权简单方法
2009-08-29 15:21:00
卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法
2011-11-03 16:49:09
SQL Join的一些总结(实例)
2012-08-21 10:19:29
Python去除列表中重复元素的方法
2021-06-23 20:44:27
淘宝2011新版首页开发实践
2011-01-20 20:07:00
python中内置库os与sys模块的详细介绍
2021-12-05 21:30:33
python实现录音功能(可随时停止录音)
2023-07-29 05:15:06
asp函数解决SQL注入漏洞
2008-10-12 19:53:00
mysql rand(x)生产重复随机序列
2010-12-03 16:01:00
JavaScript中尽量用局部变量的原因[译]
2009-02-20 13:45:00
OpenCV视频流Python多线程处理方法详细分析
2022-02-14 20:29:45
Sql Server 2005数据库被标记为“可疑”问题
2009-12-15 10:50:00