折线图
from functools import reduce
from scipy.stats.stats import mode
from scipy.sparse import coo_matrix
import scipy.sparse as sp
from numpy.random import rand
import numpy as np
import math
import matplotlib.pylab as plt
import seaborn as sns
import pandas as pd
import matplotlib
from matplotlib.pyplot import MultipleLocator
# 设置画框大小
matplotlib.rcParams['figure.figsize'] = [15, 5]
matplotlib.rcParams['savefig.dpi'] = 500
# fig, axs = plt.subplots(1,1)
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 15,
}
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 15,
}
font3 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 20,
}
plt.rc('patch',force_edgecolor=True)
#子图一绘制
plt.subplot(121)
x = np.array([1, 2, 3, 4, 5])
y1 = np.array([0.7, 0.8, 0.8, 0.8, 0.9])
y2 = np.array([0.7, 0.8, 0.8, 0.8, 0.9])
#颜色
c1 = 'blue'
c2 = 'red'
ax3 = plt.plot(x, y1, linestyle='-', marker='s',linewidth=2, color=c1, label='***')
plt.errorbar(x, y1,
yerr=[0.006,0.006,0.007,0.004,0.004],
fmt='o',ecolor=c1,color=c1,elinewidth=2,capsize=4
)
ax4 = plt.plot(x, y2, linestyle='--', marker='o',linewidth=2, color=c2, label='***' + '$\mathregular{_{sim}}$')
plt.errorbar(x, y2,
yerr=[0.003,0.004,0.007,0.005,0.005],
fmt='o',ecolor=c2,color=c2,elinewidth=2,capsize=4
)
#在折线图上标数据
for a,b in zip(x,y1):
if a == 5:
plt.text(a-0.15, b+0.003, '%.3f' % b, ha='center', va= 'bottom',fontsize=12)
else:
plt.text(a+0.15, b+0.003, '%.3f' % b, ha='center', va= 'bottom',fontsize=12)
for a,b in zip(x,y2):
if a == 5:
plt.text(a-0.15, b+0.003, '%.3f' % b, ha='center', va= 'bottom',fontsize=12)
else:
plt.text(a+0.15, b+0.003, '%.3f' % b, ha='center', va= 'bottom',fontsize=12)
plt.grid(True)
x_major_locator=MultipleLocator(1)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
#设置区间
plt.ylim(0.745, 0.86)
plt.xlim(0.9, 5.1)
#标题
plt.title('Model Depth ' + r'$L$',font3)
#字体大小
plt.tick_params(labelsize=13)
#x轴
plt.xticks(x,['layer=0', 'layer=1', 'layer=2', 'layer=3', 'layer=4'],fontsize=14)
plt.ylabel('AUC',font1)
#右上角的标注
plt.legend(loc='upper right',prop=font2, ncol=2)
##############################################################################################################
#子图二绘制 同理
plt.subplot(122)
...
plt.savefig('1.jpg', dpi=500)
效果如下:
柱状图
import matplotlib.pylab as plt
import matplotlib
from matplotlib.pyplot import MultipleLocator
# import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
matplotlib.rcParams['savefig.dpi'] = 500
# fig, axs = plt.subplots(1,1)
matplotlib.rcParams['figure.figsize'] = [10, 7]
font0 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 11,
}
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 12,
}
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 13,
}
d = pd.DataFrame()
d['variant'] = ['GCN']*15 + ['Sub-Neighbor']*15 + ['Attentive']*15 + ['Informative']*15 + ['GCN']*15 + ['Sub-Neighbor']*15 + ['Attentive']*15 + ['Informative']*15
d['p'] = ['Recall@1','Recall@15','Pre@1','Precision@15', 'AUC'] * (len(d['variant']) // 5)
d['dataset'] = ['MiGCN']*60 + ['MiGCN' + '$\mathregular{_{sim}}$']*60
d['ret'] = [0.0291,0.2158,0.0324,0.0162,0.824]\
+ [0.0291+0.0025,0.2158+0.0044,0.0324+0.003,0.0162+0.0008,0.824+0.004]\
+ [0.0291-0.0025,0.2158-0.0044,0.0324-0.003,0.0162-0.0008,0.824-0.004]\
+ [0.0296,0.2142,0.0327,0.0162,0.825]\
+ [0.0296+0.003,0.2142+0.005,0.0327,0.0162+0.0009,0.825+0.005]\
+ [0.0296-0.003,0.2142-0.005,0.0327,0.0162-0.0009,0.825-0.005]\
+ [0.0320,0.2178,0.0335,0.0165,0.827]\
+ [0.0320+0.002,0.2178+0.0036,0.0335,0.0165+0.0007,0.827+0.004]\
+ [0.0320-0.002,0.2178-0.0036,0.0335,0.0165-0.0007,0.827-0.004]\
+ [0.0322,0.2194,0.0349,0.0169,0.831]\
+ [0.0322+0.0017,0.2194+0.0040,0.0349,0.0169+0.0007,0.831+0.004]\
+ [0.0322-0.0017,0.2194-0.0040,0.0349,0.0169-0.0007,0.831-0.004]\
+ [0.0293,0.1432,0.0307,0.0102,0.780]\
+ [0.0293-0.0015,0.1432+0.0021,0.0307,0.0102+0.0009,0.780+0.005]\
+ [0.0293+0.0015,0.1432-0.0021,0.0307,0.0102-0.0009,0.780-0.005]\
+ [0.0300+0.0023,0.1459-0.0025,0.0316,0.0103,0.778+0.006]\
+ [0.0300,0.1459+0.0025,0.0316,0.0103+0.0006,0.778-0.006]\
+ [0.0300-0.0023,0.1459,0.0316,0.0103-0.0006,0.778]\
+ [0.0321+0.0020,0.1563-0.0018,0.0350,0.0105+0.0007,0.782+0.004]\
+ [0.0321-0.0020,0.1563+0.0018,0.0350,0.0105-0.0007,0.782-0.004]\
+ [0.0321,0.1563,0.0350,0.0105,0.782]\
+ [0.0313,0.1608,0.0327,0.0119,0.787]\
+ [0.0313+0.0025,0.1608+0.002,0.0327,0.0119-0.0007,0.787+0.006]\
+ [0.0313-0.0025,0.1608-0.002,0.0327,0.0119+0.0007,0.787-0.006]
r1 = d[d['p'] == 'Recall@1']
r15 = d[d['p'] == 'Recall@15']
p1 = d[d['p'] == 'Pre@1']
p15 = d[d['p'] == 'Precision@15']
dauc = d[d['p'] == 'AUC']
fig = plt.figure()
ax1 = fig.add_subplot(221)
plt.tick_params(labelsize=14)
ax2 = fig.add_subplot(222)
plt.tick_params(labelsize=14)
ax3 = fig.add_subplot(223)
plt.tick_params(labelsize=14)
y_major_locator=MultipleLocator(0.005)
ax3.yaxis.set_major_locator(y_major_locator)
ax4 = fig.add_subplot(224)
plt.tick_params(labelsize=14)
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.3, hspace=None)
# sns.set_theme(style="whitegrid",font='Times New Roman',font_scale=100)
plt.rc('patch',force_edgecolor=True)
sns.barplot(x='dataset',y='ret',data=r1,hue='variant',palette='OrRd',ax=ax1, capsize=.05, errwidth=1.5)
sns.barplot(x='dataset',y='ret',data=r15,hue='variant',palette='OrRd',ax=ax2, capsize=.05, errwidth=1.5)
sns.barplot(x='dataset',y='ret',data=p15,hue='variant',palette='OrRd',ax=ax3, capsize=.05, errwidth=1.5)
sns.barplot(x='dataset',y='ret',data=dauc,hue='variant',palette='OrRd',ax=ax4, capsize=.05, errwidth=1.5)
ax1.set_ylim(0.01, 0.05)
ax1.set_ylabel('Recall@1',font2)
ax2.set_ylim(0.12, 0.23)
ax2.set_ylabel('Recall@15',font2)
ax3.set_ylim(0.005, 0.02)
ax3.set_ylabel('Precision@15',font2)
ax4.set_ylim(0.75, 0.85)
ax4.set_ylabel('AUC',font2)
ax1.set_xlabel(None)
ax2.set_xlabel(None)
ax3.set_xlabel(None)
ax4.set_xlabel(None)
ax1.legend(loc='upper left',prop=font0, ncol=2)
ax2.legend(loc='best',prop=font1, ncol=1)
ax3.legend(loc='best',prop=font1, ncol=1)
ax4.legend(loc='best',prop=font1, ncol=1)
plt.savefig('./mp1.jpg', dpi=500)
效果如下: