AI人工智能——Matplotlib绘制各种数据可视化图表的基础方法

一、绘制图像基础

python 复制代码
import matplotlib.pyplot as plt

# 1、创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 2、绘制图像
x = [1, 2, 3, 4, 5, 6]
y = [3, 5, 4, 3, 6, 1]
plt.plot(x, y)
# 3、显示图像
plt.show()

二、图像保存

python 复制代码
import matplotlib.pyplot as plt

# 1、创建画布
plt.figure(figsize=(20,8),dpi=100)
# 2、绘制图像
x = [1,2,3,4,5,6]
y = [3,5,4,5,6,1]
plt.plot(x,y)
# 2.1、图像保存(一定要放在show前面)
plt.savefig("./data/test3.png")
# 3、显示图像
plt.show()

三、案例:图像基本绘制

python 复制代码
import matplotlib.pyplot as plt
import random

x = range(60)
y_hangzhou = [random.uniform(10, 15) for i in x]
y_shanghai = [random.uniform(15, 25) for i in x]
# 1、创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 2、绘制图像
plt.plot(x, y_hangzhou, label="杭州", color="g", linestyle="-.")
plt.plot(x, y_shanghai, label="上海")
# 2.1、添加x,y轴
y_ticks = range(40)
x_ticks_labels = ["11点{}分".format(i) for i in x]
plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5])
# 2.2、添加网格
plt.grid(True, linestyle="--", alpha=1)
# 2.3、添加描述
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("一小时温度变化图", fontsize=20)
# 2.4、显示图例
plt.legend(loc=0)
# 3、显示图像
plt.rcParams["font.sans-serif"] = ["Heiti TC"]  # 解决中文乱码
plt.rcParams["axes.unicode_minus"] = False  # 解决保存图片为框
plt.show()

四、多个坐标显示图像

python 复制代码
import matplotlib.pyplot as plt
import random

x = range(60)
y_hangzhou = [random.uniform(10, 15) for i in x]
y_shanghai = [random.uniform(15, 25) for i in x]
# # 1、创建画布
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)
# # 2、绘制图像
axes[0].plot(x, y_hangzhou, label="杭州", color="g", linestyle="-.")
axes[1].plot(x, y_shanghai, label="上海")
# # 2.1、添加x,y轴
y_ticks = range(40)
x_ticks_labels = ["11点{}分".format(i) for i in x]
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_labels[::5])
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_labels[::5])
# # 2.2、添加网格
axes[0].grid(True, linestyle="--", alpha=1)
axes[1].grid(True, linestyle="--", alpha=1)
# # 2.3、添加描述
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[0].set_title("杭州一小时温度变化图", fontsize=20)
axes[1].set_xlabel("时间")
axes[1].set_ylabel("温度")
axes[1].set_title("上海一小时温度变化图", fontsize=20)
# # 2.4、显示图例
axes[0].legend(loc=0)
axes[1].legend(loc=0)
# 3、显示图像
plt.rcParams["font.sans-serif"] = ["Heiti TC"]  # 解决中文乱码
plt.show()

五、应用场景:绘制数学图像

python 复制代码
import numpy as np
import matplotlib.pyplot as plt

# 0、生成数据
x = np.linspace(-10, 10, 100)
# y = np.sin(x)
y = x * x * x
# 1、创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 2、绘制
plt.plot(x, y)
plt.grid()
# 3、显示图像
plt.show()
复制代码
常见图形绘制 
复制代码
1.折线图--plt.plot         ——变化
2.散点图--plt.scatter()    ——分布规律
3.柱状图--plt.bar          ——统计、对比
4.直方图--plt.hist()       ——统计,分布
5.饼图--plt.pie()          ——占比

六、散点图

python 复制代码
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 3, 3, 4, 5, 7, 8, 4, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
y = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 4, 3, 2, 7, 8, 6, 4, 9, 0, 3, 4, 5, 7, 8, 1, 4, 2, 6, 7]
# 1、创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 2、图像绘制
plt.scatter(x, y)
# 3、图像展示
plt.show()

七、柱状图

python 复制代码
import matplotlib.pyplot as plt

movie_name = ['雷神3:诸神黄昏', '正义联盟', '东方快车谋杀案', '寻梦环游记', '全球风暴', '降魔传', '追捕', '七十七天',
              '密战', '狂兽', '其它']
x = range(len(movie_name))
y = [73853, 57767, 22354, 15969, 14839, 8725, 8716, 8318, 7916, 6764, 52222]
# 1、创建画布
plt.figure(figsize=(30, 8), dpi=100)
# 2、图像绘制
plt.bar(x, y, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'], width=0.5)
# 2.1、设置x轴
plt.xticks(x, movie_name, fontsize=15)
# 2.2、网格
plt.grid()
# 2.3、标题
plt.title("电影票房")
# 3、图像展示
plt.rcParams["font.sans-serif"] = ["Heiti TC"]  # 解决中文乱码
plt.rcParams["axes.unicode_minus"] = False  # 解决保存图片为框
plt.show()
相关推荐
字节跳动视频云技术团队10 小时前
接大模型别再自己配 Key、搭代理了,火山引擎 IGA Pages 一步搞定
人工智能
阿里云大数据AI技术11 小时前
EMR Serverless StarRocks 产品介绍:从统一 OLAP 走向多模态分析与检索
人工智能
Forget the Dream11 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
学无止境小袁同学11 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴12 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
塑料铲子12 小时前
你的 Skill 越多越蠢?Token 上下文爆炸的求生指南
机器学习·agent
火山引擎开发者社区12 小时前
豆包工作,3 个月,800 万,20 亿
人工智能
Chuck.li12 小时前
AI 长篇创作工具选型:工作流对照与自测清单
人工智能·大模型·ai写作·网文作者·ai网文·豆包写作
aleimu12 小时前
Windows 11 右键打开 Cmder 2.0:从找不到菜单到默认使用 Cmder
前端·人工智能·后端
LK_0712 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表