🤵 Author :Horizon John
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
🏆 神经网络篇:经典网络模型
💻 算法篇:再忙也别忘了 LeetCode
MatPlotlib 最全 使用技巧与操作指南
简单绘图
首先从创建 图形
和 维度
开始:
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颜色
线条风格
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')
sharex
和 sharey
用于关联不同子表格之间的参数
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])