MatPlotlib 最全 使用技巧与操作指南


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


简单绘图

首先从创建 图形维度 开始:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes()

plt.show()

在这里插入图片描述

图形(类plt.Figure的一个实例):包括了所有维度、图像、文本和标签对象等 ;
维度(类plt.Axes的一个实例):就是上面的图,一个有边界的格子包括刻度和标签,以及上面的图表元素 ;


定制画布风格

plt.style.use('seaborn-whitegrid')    # 画布风格

在这里插入图片描述

一共有以下这么多的画布风格:

['bmh', 'classic', 'dark_background', 'fast', 
'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 
'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid',
 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 
'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks',
 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 
'tableau-colorblind10', '_classic_test']

绘制简单函数

x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))

在这里插入图片描述

也可以直接使用 plt 函数进行会绘制;
绘制多根线条,多次调用 plt 函数即可:

plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

在这里插入图片描述


设置坐标轴

坐标轴范围

xlim()
ylim()

plt.plot(x, np.sin(x))
plt.xlim(0, 10)
plt.ylim(-1.5, 1.5)

在这里插入图片描述

坐标轴反向:将参数数据颠倒即可

plt.plot(x, np.sin(x))
plt.xlim(10, 0)
plt.ylim(1.5, -1.5)

在这里插入图片描述

也可以直接使用 plt.axis() 设置 x轴 和 y轴 的参数:

x = np.linspace(0, 10, 1000)
plt.plot(x, np.sin(x))
plt.axis([0, 10, -1.5, 1.5])

在这里插入图片描述

坐标轴标签

title()
xlabel()
ylabel()

plt.title("Line")
plt.xlabel("x")
plt.ylabel("sin(x)")

在这里插入图片描述

图例

legend()

plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.axis('equal')
plt.legend()

在这里插入图片描述

loc=0:图列位置 = ’best ’
0 ---- best自己分配最佳位置
1 ---- upper right
2 ---- upper left
3 ---- lower left
4 ---- lower right
5 ---- right
6 ---- center left
7 ---- center right
8 ---- lower center
9 ---- upper center
10 ---- center
frameon=False:边框是否显示
ncol=2:图例分栏
fontsize=8:字体大小
framealpha=0.5:图例透明度

plt.plot(x, np.sin(x), '-b', label='sin(x)')
plt.axis('equal')
plt.legend(loc=0, frameon=True, ncol=1, fontsize=8, framealpha=0.5)

在这里插入图片描述


线条颜色及风格

线条颜色

color

r : 红色 m : 洋红色
g : 绿色 y : 黄色
b : 蓝色 k : 黑色
w : 白色 c : 青绿色

RGB颜色参考:RGB颜色对照表

plt.plot(x, x)                              # 无
plt.plot(x, x + 1, color='blue')            # 颜色名称
plt.plot(x, x + 2, color='g')               # 颜色简写 (r g b w m y k c)
plt.plot(x, x + 3, color='0.75')            # 0-1之间的灰阶值
plt.plot(x, x + 4, color='#FFDD44')         # 16进制的值
plt.plot(x, x + 5, color=(1.0, 0.2, 0.3))   # RGB元组的颜色值,每个值介于0-1
plt.plot(x, x + 6, color='chartreuse')      # HTML颜色

![在这里插入图片描述](https://img-blog.csdnimg.cn/9311551558b444a7a935c11ddf2dcde8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBASG9yaXpvbiBNYXg=,size_11,color_FFFFFF,t_70,g_se,x_16

线条风格

linestyle

plt.plot(x, x, linestyle='')           # 无
plt.plot(x, x + 1, linestyle='-')      # 实线
plt.plot(x, x + 2, linestyle='--')     # 破折线
plt.plot(x, x + 3, linestyle='-.')     # 点划线
plt.plot(x, x + 4, linestyle=':')      # 虚线

在这里插入图片描述

颜色 + 风格

plt.plot(x, x + 0, '-g')      # 绿色实线
plt.plot(x, x + 1, '--c')     # 青色破折线
plt.plot(x, x + 2, '-.k')     # 黑色点划线
plt.plot(x, x + 3, ':r')      # 红色虚线

在这里插入图片描述


多个子图

表格插图

距离 x轴y轴 60% 的位置 ;
以插图的形式放置一个 宽度高度 都是 20% 子图表 ;

fig = plt.figure()
ax1 = plt.axes()                           # 标准图表
ax2 = plt.axes([0.6, 0.6, 0.2, 0.2])       # 子图表

在这里插入图片描述

堆叠子图表

fig.add_axes() 两个垂直堆叠的子图表 :

fig = plt.figure()                             # 创建figure对象
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4],
                   ylim=(-1.5, 1.5))           # x轴10%, y轴50%, 宽度80%. 高度40%
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4],
                   ylim=(-1.5, 1.5))           # x轴10%, y轴10%, 宽度80%. 高度40%

x = np.linspace(0, 10)
ax1.plot(np.sin(x))
ax2.plot(np.cos(x))

在这里插入图片描述

网格子图表

plt.subplot()

for i in range(1, 7):
    plt.subplot(2, 3, i)
    plt.text(0.5, 0.5, str((2, 3, i)),
             fontsize=18, ha='center')

网格行数,网格列数,该网格子图表的序号(从左上角向右下角递增)

在这里插入图片描述

plt.subplots_adjust() 调整子表图之间的距离

沿着 高度宽度 方向子图表之间的距离,以 40% 为例 :

fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)

for i in range(1, 7):
    plt.subplot(2, 3, i)
    plt.text(0.5, 0.5, str((2, 3, i)),
             fontsize=18, ha='center')

在这里插入图片描述

sharexsharey 用于关联不同子表格之间的参数

fig, ax = plt.subplots(2, 3, sharex='col', sharey='row')

在这里插入图片描述

不规则子图表

plt.GridSpec() 指定子图表的 位置 和 占据的 网格 ;

grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)

plt.subplot(grid[0, 0])
plt.subplot(grid[0, 1:])
plt.subplot(grid[1, :2])
plt.subplot(grid[1, 2])

在这里插入图片描述



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值