使用Matplotlib绘图


🤵 AuthorHorizon 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)

输出结果:
在这里插入图片描述



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Horizon John

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

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

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

打赏作者

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

抵扣说明:

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

余额充值