matplotlib绘图【折线图】

基础

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)
# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]
# 绘图
plt.plot(x, y)
# 展示
plt.show()

在这里插入图片描述

设置图片大小

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 设置图片大小,dpi:设置像素,越大图片越清晰
fig = plt.figure(figsize=(30,10), dpi=100)

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

保存图片

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 设置图片大小
fig = plt.figure(figsize=(30,10), dpi=100)

# 绘图
plt.plot(x, y)

# 保存图片
plt.savefig('./first.png')

# 展示
plt.show()
  • 也可以保存为svg格式,矢量图片放大不会失真

自定义x轴坐标

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
plt.xticks(x)

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

  • 改变坐标显示疏密度
from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
_xtick_labels = [i/2 for i in range(4, 49)]
plt.xticks(_xtick_labels)

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
_xtick_labels = [i/2 for i in range(4, 49)]
# 步长为3将坐标变稀疏
plt.xticks(_xtick_labels[::3])

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

设置y轴坐标

from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
_xtick_labels = [i/2 for i in range(4, 49)]
# 步长为3将坐标变稀疏
plt.xticks(_xtick_labels[::3])

# 自定义y轴坐标:以y轴数据最大值最小值为范围生成数据
plt.yticks(range(min(y), max(y)+1))

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

  • 调整坐标疏密程度
from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
_xtick_labels = [i/2 for i in range(4, 49)]
# 步长为3将坐标变稀疏
plt.xticks(_xtick_labels[::3])

# 自定义y轴坐标:以y轴数据最大值最小值为范围生成数据,按步长为3取值
plt.yticks(range(min(y), max(y)+1)[::3])

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

特殊用法

  • 如果自定义坐标值最小值大于数据最大值,那么坐标是看不到的
  • 由于x轴数据最大值是25,所以横轴坐标只能看到24,25两个值
from matplotlib import pyplot as plt

# x轴数据
x = range(2, 26, 2)

# y轴数据
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]

# 自定义x轴坐标
_xtick_labels = [i for i in range(24, 69)]
# 步长为3将坐标变稀疏
plt.xticks(_xtick_labels)

# 绘图
plt.plot(x, y)

# 展示
plt.show()

在这里插入图片描述

显示字符串

from matplotlib import pyplot as plt
import random

# 生成坐标系
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y)

# 调整x轴的疏密度以及显示字符串
_x = x
_xtick_labels = ["hello{}".format(i) for i in _x]
plt.xticks(x, _xtick_labels)

# 展示
plt.show()

在这里插入图片描述

  • 字符旋转
from matplotlib import pyplot as plt
import random

# 生成坐标系
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]

# 取步长,数字和字符串一一对应,数据长度一样  rotation(x轴显示旋转角度)
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=45)

# 展示
plt.show()

在这里插入图片描述

显示中文字符

  • matplotlib不支持中文字符,因此无法显示中文汉字
  • fc-list:linux/unix查看支持的字体
  • fc-list :lang=zh查看支持的中文(冒号前有空格)
  • 修改matplotlib默认字体
  • matplotlib.rc可以修改
  • 通过matplotlib下的font_manager可以解决(windows/linux/unix)
from matplotlib import pyplot as plt
import random

# 生成坐标系
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]

# windows和linux设置字体方式(有可能不生效)
# import matplotlib
# font = {'family': 'MicroSoft YaHei',
#         'weight': 'bold',
#         'size': 'larger'}
#
# matplotlib.rc("font", **font)
# matplotlib.rc("font", family="MicroSoft YaHei", weight="bold")

from matplotlib import font_manager
# 另一种设置字体的方式(百分百生效)
my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]

# 取步长,数字和字符串一一对应,数据长度一样  rotation(x轴显示旋转角度)
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)

# 展示
plt.show()

在这里插入图片描述

显示描述信息

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

# 生成坐标系
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]

my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]

# 取步长,数字和字符串一一对应,数据长度一样  rotation(x轴显示旋转角度)
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)

# 添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度 单位(℃)", fontproperties=my_font)
plt.title("10点到12点每分钟的气温变化情况", fontproperties=my_font)

# 展示
plt.show()

在这里插入图片描述

绘制网格

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

my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")
# 坐标轴
y = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
x = range(11, 31)

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x, _xtick_labels, fontproperties=my_font)
plt.yticks(range(0, 9))

# 绘制网格 alpha 网格透明度
plt.grid(alpha=0.3)

# 展示
plt.show()

在这里插入图片描述

多图像

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

my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")
# 坐标轴
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [1, 3, 1, 0, 2, 4, 3, 2, 1, 4, 2, 1, 3, 1, 4, 2, 3, 1, 2, 1]
x = range(11, 31)

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y_1)
plt.plot(x, y_2)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x, _xtick_labels, fontproperties=my_font)

# 绘制网格 alpha 网格透明度
plt.grid(alpha=0.3)

# 展示
plt.show()

在这里插入图片描述

添加图例

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

my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")
# 坐标轴
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [1, 3, 1, 0, 2, 4, 3, 2, 1, 4, 2, 1, 3, 1, 4, 2, 3, 1, 2, 1]
x = range(11, 31)

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.plot(x, y_1, label="自己")
plt.plot(x, y_2, label="同桌")

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x, _xtick_labels, fontproperties=my_font)

# 绘制网格 alpha 网格透明度
plt.grid(alpha=0.3)

# 添加图例 prpo:处理中文显示, loc: 图例显示位置
plt.legend(prop=my_font, loc=0)

# 展示
plt.show()

在这里插入图片描述

图表线条样式

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

my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
                                      size="x-small")
# 坐标轴
y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_2 = [1, 3, 1, 0, 2, 4, 3, 2, 1, 4, 2, 1, 3, 1, 4, 2, 3, 1, 2, 1]
x = range(11, 31)

# 设置图片大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
"""
color:线条颜色,支持RGB格式颜色
linestyle:线条风格[-实线, --虚线, -.点划线, :点虚线, ''留空或空格,无线条]
linewidth:线条粗细[浮点数]
alpha:线条透明度
"""
plt.plot(x, y_1, label="自己", color="orange", linestyle="-", linewidth=3, alpha=0.5)
plt.plot(x, y_2, label="同桌", color="blue", linestyle="--", linewidth=5, alpha=0.3)

# 调整x轴的疏密度以及显示字符串
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x, _xtick_labels, fontproperties=my_font)

# 绘制网格 alpha 网格透明度
plt.grid(alpha=0.3)

# 添加图例 loc: 图例显示位置
plt.legend(prop=my_font, loc=2)

# 展示
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值