🤵 Author :Horizon John
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
🏆 神经网络篇:经典网络模型
💻 算法篇:再忙也别忘了 LeetCode
使用Matplotlib绘图
1): plt.plot()
:曲线函数
颜色
r
: 红色 m
: 洋红色
g
: 绿色 y
: 黄色
b
: 蓝色 k
: 黑色
w
: 白色 c
: 青绿色
#008000
: RGB某颜色
风格
‐
: 实线
‐‐
: 破折线
‐.
: 点划线
:
: 虚线
: 无线条
标记
.
: 点标记
,
: 像素标记(极小点)
o
: 实心圈标记
v
: 倒三角标记
^
: 上三角标记
>
: 右三角标记
<
: 左三角标记
2): plt.bar()
:柱状图
3): plt.xlabel()
:坐标
4): plt.title()
:图表标题
5): plt.grid()
:网格显示
6): plt.legend()
:图例
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
plt.legend(loc=7, frameon=False, ncol=1, fontsize=8, framealpha=0.5)
loc=7
:图列位置 = ’ center right ’
frameon=False
:边框是否显示
ncol=2
:图例分栏
fontsize=8
:字体大小
framealpha=0.5
:图例透明度
示例1——曲线图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2,100)
y1 = x
y2 = x**2
y3 = x**3
plt.plot(x, y1, 'r-', label='linear')
plt.plot(x, y2, 'g-.', label='quadratic')
plt.plot(x, y3, 'b--', label='cubic')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Example for draw lines')
plt.grid(True)
plt.legend(loc='upper right')
plt.show()
输出结果:
示例2——柱状图
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
def draw(acc1, acc2, acc3, acc4):
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.arange(4)
bar_width = 0.15
tick_label = ['Model1', 'Model2', 'Model3', 'Model4']
plt.figure(figsize=(10, 6))
plt.bar(x-3*bar_width/2, acc1, bar_width, align="center", color='orange', label='acc1', alpha=0.5)
plt.bar(x-bar_width/2, acc2, bar_width, color='m', align="center", label='acc2', alpha=0.5)
plt.bar(x+bar_width/2, acc3, bar_width, color='b', align="center", label='acc3', alpha=0.5)
plt.bar(x+3*bar_width/2, acc4, bar_width, color='r', align="center", label='acc4', alpha=0.5)
for j in range(4):
plt.text(j-3*bar_width/2, acc1[j], str('{:.2f}'.format(adver_acc[j])+'%'),
fontsize=8, color='#EE00EE', ha='center', va='bottom')
plt.text(j-bar_width/2, acc2[j], str('{:.2f}'.format(test_acc[j]) + '%'),
fontsize=8, color='#EE00EE', ha='center', va='bottom')
plt.text(j+bar_width/2, acc3[j], str('{:.2f}'.format(train_acc[j]) + '%'),
fontsize=8, color='#EE00EE', ha='center', va='bottom')
plt.text(j+3*bar_width/2, acc4[j], str('{:.2f}'.format(clean_acc[j]) + '%'),
fontsize=8, color='#EE00EE', ha='center', va='bottom')
plt.title('Models and Accuracy')
plt.xticks(x, tick_label)
plt.xlabel("Models")
plt.ylabel("Accuracy")
plt.legend(loc=2, frameon=False, fontsize=8, framealpha=0.5)
plt.show()
acc1 = [18.630, 90.050, 5.110, 89.850]
acc2 = [85.310, 91.790, 85.310, 90.480]
acc3 = [53.258, 97.873, 45.701, 96.580]
acc4 = [97.730, 91.066, 97.730, 88.822]
draw(acc1, acc2, acc3, acc4)
输出结果: