Python AttributeError: 'Figure' 对象没有属性 'plot'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38701137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:19:07  来源:igfitidea点击:

AttributeError: 'Figure' object has no attribute 'plot'

pythonmatplotlib

提问by Richard Rublev

My code

我的代码

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,Hymanknife,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, Hymanknife[:, 0], Hymanknife[:, 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() 

The problem is with the plotting part of my code.What should I change?I am using Python 2.7 on Ubuntu.

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

采纳答案by Suever

You assign ax2to a figureobject which doesn't have a plotmethod defined. You want to create your axes using plt.axesinstead

您分配ax2figure没有plot定义方法的对象。你想创建一个使用您的轴plt.axes,而不是

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

回答by Julio C. Kota Renteria

instead of calling the figure with:

而不是调用图形:

ax2 = fig #which just references the figure

try using gca()to extract the axes:

尝试使用gca()提取轴:

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