AI——使用matplotlib

一、matplotlib图像结构

在这里插入图片描述

二、基础绘图功能(单个坐标系)

from matplotlib import pyplot as plt
from matplotlib import font_manager
import random

# 获取系统上已经安装的字体
a = sorted([f.name for f in font_manager.fontManager.ttflist])
# print(a)

# 解决中文乱码的问题
plt.rcParams['font.sans-serif']=['FangSong']
# 解决图像中的"-"负号的乱码问题
plt.rcParams["axes.unicode_minus"]=False 

# 1.准备数据
# 1.1 获取一个0-59的数组
x = range(60)
# 1.2 通过该x轴,获取y轴数据,y轴为15-18的随机值
y = [random.uniform(15, 18) for i in x]

# 1.3 构建一个北京的温度折线图
y_beijing = [random.uniform(1, 3) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3.绘制折线图
plt.plot(x, y, label="默认")
plt.plot(x, y_beijing, color="r", linestyle="--", label="北京")

# 3.1 添加x,y轴刻度
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(40)
y_ticks_label = ["{}度".format(i) for i in y_ticks]

# 修改x,y轴坐标刻度显示
plt.xticks(x[::5], x_ticks_label[::5], fontsize=20)
plt.yticks(y_ticks[::5], y_ticks_label[::5], fontsize=20)

# 3.2 添加图像背景网格
plt.grid(True, linestyle="--", alpha=0.5)

# 3.3 添加描述信息
plt.xlabel("时间",fontsize=50)
plt.ylabel("温度", fontsize=50)
plt.title("中午11点-12点某城市温度变化图", fontsize=50)

# 3.4 图像保存
plt.savefig("./test.png")

# 3.5 显示图例
plt.legend(loc=0,fontsize=40)

# 4.显示图像
plt.show()

在这里插入图片描述

  • 设置图像风格
    在这里插入图片描述
  • 显示图例
    在这里插入图片描述

三、多个坐标系实现绘图

from matplotlib import pyplot as plt
from matplotlib import font_manager
import random

# 获取系统上已经安装的字体
a = sorted([f.name for f in font_manager.fontManager.ttflist])
# print(a)

# 解决中文乱码的问题
plt.rcParams['font.sans-serif']=['FangSong']
# 解决图像中的"-"负号的乱码问题
plt.rcParams["axes.unicode_minus"]=False 

# 1.准备数据
# 1.1 获取一个0-59的数组
x = range(60)
# 1.2 通过该x轴,获取y轴数据,y轴为15-18的随机值
y = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]

# 1.3 获取x轴和y轴的刻度
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks_label = ["{}度".format(i) for i in range(40)]

# 2. 创建画布
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)

# 3. 绘制图像
axes[0].plot(x, y, label="默认")
axes[1].plot(x, y_beijing, color="r", linestyle="--", label="北京")

# 3.1 添加x,y轴刻度
# 第一个坐标系
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_label[::5])
axes[0].set_yticklabels(y_ticks_label[::5])
# 第二个坐标系
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_label[::5])
axes[1].set_yticklabels(y_ticks_label[::5])

# 3.2 添加网格显示
axes[0].grid(True, linestyle="--", alpha=1)
axes[1].grid(True, linestyle="-.", alpha=1)

# 3.3 添加描述信息
axes[0].set_xlabel("时间", fontsize=40)
axes[0].set_ylabel("温度", fontsize=40)
axes[0].set_title("11点-12点某城市温度变化图", fontsize=50)
axes[1].set_xlabel("时间", fontsize=40)
axes[1].set_ylabel("温度", fontsize=40)
axes[1].set_title("11点-12点上海温度变化图", fontsize=50)

# 3.4 图像保存
plt.savefig("./test.png")

# 3.5 显示图例
axes[0].legend(loc=0, fontsize=40)
axes[1].legend(loc=0, fontsize=40)

# 3.图像显示
plt.show()

在这里插入图片描述

四、使用numpy和matplotlib显示一个sin函数图

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import font_manager

# 1.准备数据,获取-10到10连续的1000个数
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3.绘制函数图像
plt.plot(x, y)

# 3.1 添加网格显示
plt.grid()

# 4. 显示图像
plt.show()

在这里插入图片描述

五、其他图形绘制

  • 绘制散点图
from matplotlib import pyplot as plt
from matplotlib import font_manager
import random

# 1. 准备数据
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64, 
     163.56, 120.06, 207.83, 342.75, 147.9, 53.06, 224.72, 29.51, 
     21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9, 239.34,
    140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1,
    30.74, 400.02, 205.35, 330.64, 283.45]

# 2. 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3. 绘制散点图
plt.scatter(x, y)

# 4. 显示图像
plt.show()

在这里插入图片描述

  • 绘制柱状图
from matplotlib import pyplot as plt
from matplotlib import font_manager

# 1. 准备数据
# 电影名称
movie_name = ['雷神3: 诸神黄昏', '正义联盟', '东方快车谋杀案', '寻梦环游记', '全球风暴', '降魔传', '追捕', '七十七天', '密战', '狂兽', '其他']

# 横坐标
x = range(len(movie_name))

# 票房数据
y = [73853, 57767, 22354, 15969, 14839, 8725, 8716, 8318, 7916, 6764, 52222]

# 2. 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3. 绘制柱状图
plt.bar(x, y, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'], width=0.7)

# 3.1 修改x轴显示
plt.xticks(x, movie_name)

# 3.2 添加网格
plt.grid(linestyle="--", alpha=0.8)

# 3.3 添加标题
plt.title("电影票房收入对比", fontsize=50)


# 4. 图像显示
plt.show()

在这里插入图片描述

  • 绘制饼图
from matplotlib import pyplot as plt
from matplotlib import font_manager

# 1. 准备数据
x = [1, 2, 3, 4, 5]
labels = ['class1','class2','class3','class4','class5']
# 某一块饼图距离圆心的距离
explode = [0, 0, 0.1, 0, 0]

# 2. 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 3. 绘制柱状图
plt.pie(x, labels=labels, autopct='%.2f%%', explode=explode)

# 3.1 设置x,y轴等长
plt.axis("equal")

# 3.2 添加标题
plt.title("绘制饼图", fontsize=50)


# 4. 图像显示
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值