matplotlib绘图柱状图和折线图

这篇博客展示了如何使用Python的matplotlib库创建柱状图和折线图。柱状图用于展示不同年份的漏洞数量,而折线图则对比了多个参数N下不同数据集的Hit@10(Raw) (%)表现。文章通过设置图形属性、添加箭头和坐标轴隐藏等技巧,使得图表更加专业且易于理解。

柱状图:

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 这两行代码解决 plt 中文显示的问题
my_font= FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=14)


waters = ('2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019')
buy_number = [32, 200, 182, 117, 134, 116, 177, 277, 490, 441]

plt.figure(figsize=(9.9,6))

ax = plt.axes()


ax.arrow(-0.72, 512, 0, 0.01, head_width=0.13, head_length=7, fc='k', ec='k',clip_on = False,length_includes_head= True)


ax.arrow(9.7, 0.3, 0.09, 0, head_width=8,
         head_length=0.09,fc='k', ec='k',length_includes_head= True, clip_on = False)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)


plt.bar(waters, buy_number, width=0.5, alpha = .7)

plt.xlabel('年份',FontProperties=my_font,fontsize=18)
plt.ylabel('漏洞数量',FontProperties=my_font,fontsize=18)

plt.yticks(fontproperties = 'Times New Roman', size = 18)
plt.xticks(fontproperties = 'Times New Roman', size = 18)

plt.show()

折线图:

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties


# 解决 plt 中文显示的问题
my_font= FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=14)

canshu = ['0.25', '0.275', '0.3', '0.325', '0.35', '0.375', '0.4', '0.425', '0.5', '0.525']
FB15K = [0.639, 0.637, 0.646, 0.65, 0.678, 0.733, 0.761, 0.697, 0.65, 0.623]
WN18RR = [0.497, 0.495, 0.49, 0.482, 0.512, 0.498, 0.473, 0.486, 0.472, 0.466]
WN18 = [0.909, 0.918, 0.914, 0.921, 0.915, 0.92, 0.949, 0.923, 0.92, 0.925]
ICS = [0.64, 0.641, 0.672, 0.681, 0.72, 0.691, 0.685, 0.67, 0.66, 0.621]

plt.figure(figsize=(10,6))

ax = plt.axes()


ax.arrow(-0.44, 1, 0, 0.01, head_width=0.15, head_length=0.008, fc='k', ec='k',clip_on = False,length_includes_head= True)


ax.arrow(9.4, 0.3, 0.09, 0, head_width=0.01,
         head_length=0.09,fc='k', ec='k',length_includes_head= True, clip_on = False)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)



plt.ylim(0.3, 1)
plt.plot(canshu, FB15K, '.-', label = 'FB15K')
plt.plot(canshu, WN18RR, '.-', label = 'WN18RR')
plt.plot(canshu, WN18, '.-', label = 'WN18')
plt.plot(canshu, ICS, '.-', label = 'ICS')

plt.xticks(canshu, fontproperties = 'Times New Roman', size = 18)
plt.yticks(fontproperties = 'Times New Roman', size = 18)

plt.xlabel('参数N', FontProperties=my_font,fontsize=18)
plt.ylabel('Hit@10(Raw) (%)', fontproperties = 'Times New Roman',fontsize=18)
plt.legend()

plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值