Python绘制论文中的图形

编程入门 行业动态 更新时间:2024-10-26 06:33:19

Python绘制论文中的<a href=https://www.elefans.com/category/jswz/34/1770818.html style=图形"/>

Python绘制论文中的图形

一、条形图

  1. 使用场景:对多个实验方法的性能进行比较。
  2. 代码:
#条形图
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')

二、折线图

  1. 使用场景:对比趋势
  2. 代码:
#折线图
import matplotlib.pyplot as pltdata = [[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']=Falsefig = plt.figure(figsize=(5, 4))
fs = 17
lw = 1.5ax1 = 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')

三、箱线图

  1. 使用场景:显示数据分散情况的统计图。
  2. 代码:
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')

更多推荐

Python绘制论文中的图形

本文发布于:2023-12-03 20:28:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1657311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   论文   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!