我有一个EC2实例,其中包含所有需要安装和升级的东西。

尝试创建绘图时,我总是收到警告和错误。

警告是:

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.


此后立即出现的错误:

Traceback (most recent call last):
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/ec2-user/mnn/nn_tp_thread.py", line 241, in run
    plt.plot(mean_prediction_last_10)
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3347, in plot
    ax = gca()
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 984, in gca
    return gcf().gca(**kwargs)
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 1817, in gca
    return self.add_subplot(1, 1, 1, **kwargs)
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 1240, in add_subplot
    self._axstack.add(key, a)
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 142, in add
    Stack.remove(self, (key, a_existing))
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 1254, in remove
    raise ValueError('Unknown element o')
ValueError: Unknown element o


我的代码是:

            thread_locker.acquire()

            mean_prediction_last_10 = mean_prediction[9:]
            mean_prediction_last_10
            plt.plot(mean_prediction_last_10)
            plt.title('Price Prediction - ' + str(datetime.datetime.now().time()))
            plt.xlabel('Time')
            plt.ylabel('Mean Price')
            plt.legend()
            # plt.show()
            fig = plt.figure()
            fig.savefig("/static/images/Last_prediction_"+ timeframe +".png")
            thread_locker.release()


我已经搜索并尝试了许多方法,但是没有成功。有什么建议么?

编辑:
更改代码以清除任何图形和图解(尽管不应该存在):

            thread_locker.acquire()
            plt.clf()
            plt.close()
            mean_prediction_last_10 = mean_prediction[9:]
            plt.plot(mean_prediction_last_10)
            fig = plt.figure()
            plt.title('Price Prediction - ' + str(datetime.datetime.now().time()))
            plt.xlabel('Time')
            plt.ylabel('Mean Price')
            plt.legend()
            # plt.show()

            fig.savefig("/static/images/Last_prediction_"+ timeframe +".png")
            thread_locker.release()


我收到如下消息:

No handles with labels found to put in legend.


和另一个错误:

Traceback (most recent call last):
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/ec2-user/mnn/nn_tp_thread.py", line 237, in run
    plt.clf()
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 686, in clf
    gcf().clf()
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 1413, in clf
    self.delaxes(ax)         # removes ax from self._axstack
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 1007, in delaxes
    self._axstack.remove(ax)
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 113, in remove
    Stack.remove(self, self._entry_from_axes(a))
  File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/matplotlib/figure.py", line 108, in _entry_from_axes
    ind, k = {a: (ind, k) for k, (ind, a) in self._elements}[e]
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x7f165b4e10b8>

最佳答案

当我尝试使用matplotlib获取频谱图数据时,我也遇到了一个错误“未知元素o”:

pxx, freqs, bins, im = plt.specgram(sample,  NFFT=256, Fs=10000, noverlap=120)


我在训练模型时使用了这条线,所以不需要绘图,而是通过关闭窗口并移除轴来解决它:

pxx, freqs, bins, im = plt.specgram(sample,  NFFT=256, Fs=10000, noverlap=120)
plt.axis('off')
plt.close()


我希望有人能对此有所帮助。

关于python - 在EC2实例中调用图时,“未知元素o”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50119460/

10-12 18:00