一、条形图
- 使用场景:对多个实验方法的性能进行比较。
- 代码:
#条形图
import matplotlib.pyplot as plt
import numpy as np
#实验数据,每一行代表一个method,每一列代表一个性能指标
dataacc = [[0.9504, 0.9315, 0.9420, 0.9409],
[0.9223, 0.9231, 0.9314, 0.9220],
[0.8926, 0.9005, 0.9075, 0.9197]]
#横坐标的标签
tick_label=["Accuracy", "Precision", "Recall", "F1-score"]
x = np.arange(4)
#设置字体
plt.rcParams['font.sans-serif'] = ['Times New Roman']
plt.rcParams['axes.unicode_minus']=False
#字体大小
fs = 17
#设置画布大小
fig = plt.figure(figsize=(6, 4))
#绘制图形
plt.bar(x-0.125, dataacc[0], width = 0.125, color='white', edgecolor='red', hatch='//',zorder = 0, label = "method1")
plt.bar(x, dataacc[1], width = 0.125, color='white', hatch='\\\\', edgecolor='green',zorder = 0, label = "method2")
plt.bar(x+0.125, dataacc[2], width = 0.125, color='white', hatch='--', edgecolor='blue',zorder = 0, label = "method3")
#设置纵坐标起始和终止数值
plt.ylim((0.75, 1))
plt.tick_params(labelsize=fs-2)
plt.grid(True, linestyle='--', alpha=0.5)
#设置图例,loc设置位置,ncol设置列数
plt.legend(loc = 1 ,ncol=3,fontsize=fs-4)
plt.xticks(x,tick_label)
plt.xlabel("matrics",fontsize=fs)
plt.ylabel("performance", fontsize=fs)
#保存图形为pdf
plt.savefig('../bar_pic.pdf', format='pdf', dpi=400, bbox_inches='tight')
二、折线图
- 使用场景:对比趋势
- 代码:
#折线图
import matplotlib.pyplot as plt
data = [[0.9840, 0.9741, 0.9845],
[0.9631, 0.9386, 0.9428],
[0.9387, 0.9051, 0.9142],
[0.9030, 0.8835, 0.8744],
[0.8950, 0.8424, 0.8696]]
plt.rcParams['font.sans-serif'] = ['Times New Roman']
plt.rcParams['axes.unicode_minus']=False
fig = plt.figure(figsize=(5, 4))
fs = 17
lw = 1.5
ax1 = fig.add_subplot(111)
ax1.plot(["1.0", "2.0", "3.0", "4.0", "5.0"], [x[0] for x in data], c = "black", marker='+', linewidth=lw, label = "method1")
ax1.plot(["1.0", "2.0", "3.0", "4.0", "5.0"], [x[1] for x in data], c = "brown", marker='o', linewidth=lw, label = "method2")
ax1.plot(["1.0", "2.0", "3.0", "4.0", "5.0"], [x[2] for x in data], c = "darkviolet", marker='v', linewidth=lw, label = "method3")
ax1.set_ylim([0.7, 1])
ax1.tick_params(labelsize=fs-2)
ax1.set_ylabel('Accuracy', fontsize=fs)
ax1.set_xlabel('Time', fontsize=fs)
plt.grid(True, linestyle='--', alpha=0.5)
plt.legend(loc = 0 ,fontsize=fs-2)
plt.savefig('../line_pic.pdf', format='pdf', dpi=1200, bbox_inches='tight')
三、箱线图
- 使用场景:显示数据分散情况的统计图。
- 代码:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#箱线图
x = [data1,data2,data3,data4]
plt.boxplot(x,
widths=0.6,
flierprops={"marker": "*",
"markerfacecolor": "red",
"markeredgecolor":"none",
"markersize":"5"},
labels=['Training set URL','Training set embedding','Testing set URL','Testing set embedding'])
# 添加标题和标签
plt.grid(linestyle="--", alpha=0.3)
plt.savefig('../box_pic.pdf', format='pdf', dpi=1200, bbox_inches='tight')
本文含有隐藏内容,请 开通VIP 后查看