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()
相关推荐
IT_陈寒几秒前
Java序列化踩坑记:对象循环引用让我加班两小时
前端·人工智能·后端
ZhengEnCi3 分钟前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
vilya9 分钟前
Agent 语音引擎换心记:从「够用」到「稳且好」
人工智能·agent
王中阳Go10 分钟前
5.27 分钟干完后端和全部测试:一场三小时笔试的五个失分点,四个不在代码上
大数据·数据库·人工智能·后端·大语言模型
70后写java的老码农11 分钟前
Ollama 大模型私有化部署与彻底卸载指南(Windows/Linux)
人工智能
清如许.15 分钟前
如何评估 AI 智能体(AI Agent)?指标、方法与最佳实践
人工智能
猪八宅百炼成仙15 分钟前
让 AI 帮你清理电脑而不翻车:一个 Windows 体检调优 Skill 的工程化设计
人工智能·windows
宋艳_AI应用开发17 分钟前
五个阶段跑完,它交了一份日期错两年的投资报告——深思熟虑的Agent最危险的地方,是想得太像
人工智能
用户13571539981922 分钟前
凌晨三点,第 4 个反编译窗口
人工智能
Flynn Liang23 分钟前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai