用python和matplotlib画双坐标系曲线

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Geoffrey_MT/article/details/82734280

 有时候写论文的时候不免会遇到画曲线图,而且还有可能需要画双坐标系(双Y轴)的曲线图,在百度中找了许久,先mark一下,以下代码就是我论文中用到的,以后应该还会用到,先保存在这里。结果图如下:

import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio

x = np.arange(0., 1000, 2, dtype=np.int32)
xx = np.arange(0., 2000, 4, dtype=np.int32)
data = sio.loadmat('C://Users//gmt//Desktop//bn.mat')
y1 = data["loss"][:1000, 0]
y2 = data["testacc"][:1000, 0]
data = sio.loadmat('C://Users//gmt//Desktop//wobn.mat')
y11 = data["loss"][:1000, 0]
y22 = data["testacc"][:1000, 0]

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(xx, y1[x], color='red', label='Loss with BN')
ax1.plot(xx, y11[x], color='green', label='Loss without BN')
ax1.set_ylabel('Cross Entropy Loss')
ax1.set_xlabel('Iterations')
ax1.set_ylim(0, 1500)
ax1.set_xlim(0, 1000)
plt.legend(loc='lower left')


ax2 = ax1.twinx()
ax2.plot(xx, y2[x], color='blue', label='Test Accuracy with BN')
ax2.plot(xx, y22[x], color='skyblue', label='Test Accuracy without BN')
ax2.set_ylabel('Testset Accuracy')
ax2.set_xlabel('Iterations')
ax2.set_ylim(0, 1)
ax2.set_xlim(0, 1000)
plt.legend(loc='upper right')
plt.show()

猜你喜欢

转载自blog.csdn.net/Geoffrey_MT/article/details/82734280
今日推荐