AttributeError:'AxesSubplot'对象没有属性'xlabel'

时间:2017-08-22 16:06:27

标签: python matplotlib

升 必须绘制在for循环后保存,其中l获取所有值。这是我的代码。 但是我在行fig1, ax1 = plt.subplots()

上收到错误
g_i= []
g_train_loss= []
g_valid_loss=[]
g_train_accuracy=[]
g_valid_accuracy=[]
for epoch in range(niter):
        #other computations
        g_i = np.append(g_i, epoch)
        g_train_loss = np.append(g_train_loss, loss_avg.val())
        g_train_accuracy = np.append(g_train_accuracy,accuracy_train)
        g_valid_loss = np.append(g_valid_loss, cost_val)
        g_valid_accuracy = np.append(g_valid_accuracy, accuracy_valid)
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
ax1.plot(g_i,g_train_loss,label='train_loss')
ax1.plot(g_i,g_valid_loss,label='valid_loss')
ax1.xlabel("epoch")
ax1.ylabel("loss")
f1=ax1.figure()
f1.savefig('loss.png', bbox_inches='tight')
ax2.plot(g_i,g_train_accuracy,label='train_acc')
ax2.plot(g_i,g_train_accuracy,label='valid_acc')
ax2.xlabel("epoch")
ax2.ylabel("accuracy")
f2=ax2.figure()
f2.savefig('acc.png', bbox_inches='tight')

我在

处收到以下错误
fig1, ax1 = plt.subplots()

是:

/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 144, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

Edit1: 我用ubuntu 16.04 aws亚马逊服务器 我做了以下事情:

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

然后我得到以下新错误:     ax1.xlabel( “历元”) AttributeError:'AxesSubplot'对象没有属性'xlabel'

0 个答案:

没有答案