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()
相关推荐
爬虫程序猿1 小时前
用 Python 给京东商品详情做“全身 CT”——可量产、可扩展的爬虫实战
开发语言·爬虫·python
诗句藏于尽头1 小时前
自动签到之实现掘金模拟签到
python·1024程序员节
FlagOS智算系统软件栈2 小时前
与创新者同频!与FlagOS共赴开源之约
人工智能·ai·开源
蓝纹绿茶3 小时前
bash:**:pip:***python: 错误的解释器: 没有那个文件或目录
开发语言·python·pip
加油吧zkf3 小时前
循环神经网络 RNN:从时间序列到自然语言的秘密武器
人工智能·rnn·自然语言处理
START_GAME3 小时前
深度学习Diffusers:用 DiffusionPipeline 实现图像生成
开发语言·python·深度学习
Deamon Tree4 小时前
后端开发常用Linux命令
linux·运维·python
koo3645 小时前
李宏毅机器学习笔记30
人工智能·笔记·机器学习
长桥夜波5 小时前
机器学习日报02
人工智能·机器学习·neo4j
Elastic 中国社区官方博客5 小时前
介绍 Elastic 的 Agent Builder - 9.2
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索