【问题标题】:AttributeError: 'Figure' object has no attribute 'plot'AttributeError:“Figure”对象没有属性“plot”
【发布时间】:2018-12-07 02:28:27
【问题描述】:

我的代码

import matplotlib.pyplot as plt
plt.style.use("ggplot")
import numpy as np
from mtspec import mtspec
from mtspec.util import _load_mtdata

data = np.loadtxt('262_V01_C00_R000_TEx_BL_4096H.dat')

spec,freq,jackknife,f_statistics,degrees_of_f = mtspec(data=data, delta= 4930.0, time_bandwidth=4 ,number_of_tapers=5, nfft= 4194304, statistics=True)


fig = plt.figure()      
ax2 = fig   
ax2.plot(freq, spec, color='black')
ax2.fill_between(freq, jackknife[:, 0], jackknife[:, 1],color="red", alpha=0.3)
ax2.set_xlim(freq[0], freq[-1])
ax2.set_ylim(0.1E1, 1E5)
ax2.set_xlabel("Frequency $")
ax2.set_ylabel("Power Spectral Density $)")
plt.tight_layout()
plt.show() 

问题在于我的代码的绘图部分。我应该改变什么?我在 Ubuntu 上使用 Python 2.7。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您将ax2 分配给没有定义plot 方法的figure 对象。您想改为使用 plt.axes 创建轴

    ax2 = plt.axes()
    # Instead of ax2 = fig
    

    【讨论】:

      【解决方案2】:

      而不是用:

      ax2 = fig #which just references the figure
      

      尝试使用gca() 提取坐标区:

      ax2 = fig.gca() #which is used to extract the axes
      

      【讨论】:

      • gca 代表“获取当前轴”
      猜你喜欢
      • 2014-07-06
      • 2020-01-07
      • 2019-11-27
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多