# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import matplotlib.path as mpath
fig = plt.figure()
ax = fig.add_subplot(221, aspect='auto')#2*2的画布,放在第一个位置
ax.grid(True) # 隐藏网格
ax.grid(False)
rc('mathtext', default='regular')
star = mpath.Path.unit_regular_star(6) # 星型Path
circle = mpath.Path.unit_circle() # 圆形Path
# 整合两个路径对象的点
verts = np.concatenate([circle.vertices, star.vertices[::-1, ...]])
# 整合两个路径对象点的类型
codes = np.concatenate([circle.codes, star.codes])
# 根据路径点和点的类型重新生成一个新的Path对象
cut_star = mpath.Path(verts, codes) # 五角星
# rectangle = mpath.Path.unit_rectangle() # 长方形Path
# circle = mpath.Path.unit_circle() # 圆形Path
# # 整合两个路径对象的点
# rectangle_verts = np.concatenate([circle.vertices, rectangle.vertices[::-1, ...]])
# # 整合两个路径对象点的类型
# rectangle_codes = np.concatenate([circle.codes, rectangle.codes])
# # 根据路径点和点的类型重新生成一个新的Path对象
# rectangle_star = mpath.Path(rectangle_verts, rectangle_codes) # 长方形
layer = ['1', '2', '3', '4']
print(layer)
AUC = [0.8624, 0.8838, 0.8859, 0.8881]
print(AUC)
Logloss = [0.2119, 0.2209, 0.2265, 0.2307]
print(Logloss)
ax.plot(layer, Logloss, '--r', marker=star, markersize=10, label='Logloss')
ax2 = ax.twinx()
ax2.plot(layer, AUC, '--b', marker=star, markersize=10, label='AUC')
ax.legend(loc="upper left")
ax.set_xlabel("number of layer")
ax.set_ylabel("Logloss")
ax2.set_ylabel("AUC")
ax2.set_ylim(0.86, 0.9)
ax.set_ylim(0.2, 0.24)
ax2.legend(loc=0)
plt.savefig('0.png')
有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对 legend 位置进行调整。
plt.legend(loc='String or Number', bbox_to_anchor=(num1, num2))
其中,第一个参数 loc,设置它可以遵循以下的表格
String Number
upper right 1
upper left 2
lower left 3
lower right 4
right 5
center left 6
center right 7
lower center 8
upper center 9
center 10
图例在表格外
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib import rc
import matplotlib.path as mpath
fig = plt.figure('Number of layers')
ax = fig.add_subplot(221, aspect='auto')
ax.grid(True) # 隐藏网格
ax.grid(False)
rc('mathtext', default='regular')
star = mpath.Path.unit_regular_star(6) # 星型Path
circle = mpath.Path.unit_circle() # 圆形Path
# 整合两个路径对象的点
verts = np.concatenate([circle.vertices, star.vertices[::-1, ...]])
# 整合两个路径对象点的类型
codes = np.concatenate([circle.codes, star.codes])
# 根据路径点和点的类型重新生成一个新的Path对象
cut_star = mpath.Path(verts, codes) # 五角星
# layer = ['1 ', '2 ', '3 ', '4 ']
# print(layer)
# AUC = [0.8795, 0.8803, 0.881, 0.863]
# Logloss = [0.2383, 0.2376, 0.2370, 0.2532]
# plt.xticks(rotation=15)
# ax.plot(layer, Logloss, '--r', marker=star, markersize=10, label='Logloss')
# ax2 = ax.twinx()
# ax2.plot(layer, AUC, '--b', marker=star, markersize=10, label='AUC')
# # ax.legend(loc=1)
# box = ax.get_position()
# ax.set_position([box.x0, box.y0, box.width , box.height])
# ax.legend(loc='center left', bbox_to_anchor=(0, 1.12))
# ax2.set_ylim(0.85, 0.866)
# ax.set_xlabel("Number of layers")
# ax.set_ylabel("Logloss")
# ax2.set_ylabel("AUC")
# ax2.set_ylim(0.86, 0.9)
# ax.set_ylim(0.23, 0.26)
# # ax2.legend(loc=2)
# box = ax2.get_position()
# ax2.set_position([box.x0, box.y0, box.width, box.height])
# ax2.legend(loc='center right', bbox_to_anchor=(1, 1.12))
# plt.savefig('long-term-Number of layers.png')
# layer = ['16-8-4 ', '32-16-8 ', '64-32-16 ', '128-64-32']
# print(layer)
# AUC = [0.8646, 0.8654, 0.8651, 0.8417]
# Logloss = [0.2495, 0.2493, 0.2517, 0.3129]
# plt.xticks(rotation=15)
# ax.plot(layer, Logloss, '--r', marker=star, markersize=10, label='Logloss')
# ax2 = ax.twinx()
# ax2.plot(layer, AUC, '--b', marker=star, markersize=10, label='AUC')
# # ax.legend(loc=0)
# box = ax.get_position()
# ax.set_position([box.x0, box.y0, box.width , box.height])
# ax.legend(loc='center left', bbox_to_anchor=(0, 1.12))
# ax.set_xlabel("Number of neurons per layer")
# ax.set_ylabel("Logloss")
# ax2.set_ylabel("AUC")
# ax2.set_ylim(0.84, 0.866)
# ax.set_ylim(0.24, 0.32)
# # ax2.legend(loc=0)
# box = ax2.get_position()
# ax2.set_position([box.x0, box.y0, box.width , box.height])
# ax2.legend(loc='center right', bbox_to_anchor=(1, 1.12))
# plt.savefig('long-term-the number of neurons.png')
layer = ['sigmoid ', 'tanh ', 'relu ', 'leakyRelu']
print(layer)
AUC = [0.8646, 0.8556, 0.8631, 0.8654]
Logloss = [0.2512, 0.2635, 0.2501, 0.2493]
plt.xticks(rotation=15)
ax.plot(layer, Logloss, '--r', marker=star, markersize=10, label='Logloss')
ax2 = ax.twinx()
ax2.plot(layer, AUC, '--b', marker=star, markersize=10, label='AUC')
# ax.legend(loc=3)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height])
ax.legend(loc='center left', bbox_to_anchor=(0, 1.12))
ax.set_xlabel("Activation functions")
ax.set_ylabel("Logloss")
ax2.set_ylabel("AUC")
box = ax2.get_position()
ax2.set_position([box.x0, box.y0, box.width, box.height])
ax2.legend(loc='center right', bbox_to_anchor=(1, 1.12))
ax2.set_ylim(0.85, 0.866)
ax.set_ylim(0.249, 0.265)
# ax2.legend(loc=4)
plt.savefig('long-term-activation functions.png')