Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matplotlib错误:';str';对象没有属性';获取图';_Matplotlib_Error Handling_Axis Labels - Fatal编程技术网

Matplotlib错误:';str';对象没有属性';获取图';

Matplotlib错误:';str';对象没有属性';获取图';,matplotlib,error-handling,axis-labels,Matplotlib,Error Handling,Axis Labels,我的代码如下所示,我在使用ltycs.plot(ax='time')的行中遇到了一个我不理解的错误: 变量“ltycs”是一个熊猫系列,如下所示: ltycs Out[382]: time 2020-01-01 7.411365 2020-02-01 7.193070 2020-03-01 7.397183 2020-04-01 7.684527 2020-05-01 7.670577 2020-06-01 7.348572 2020-07-01 6.

我的代码如下所示,我在使用ltycs.plot(ax='time')的行中遇到了一个我不理解的错误:

变量“ltycs”是一个熊猫系列,如下所示:

ltycs
Out[382]: 
time
2020-01-01    7.411365
2020-02-01    7.193070
2020-03-01    7.397183
2020-04-01    7.684527
2020-05-01    7.670577
2020-06-01    7.348572
2020-07-01    6.898480
2020-08-01    6.852384
2020-09-01    7.250651
2020-10-01    7.681693
2020-11-01    7.587329
2020-12-01    7.444730
dtype: float64
我在图中得到了结果,我正在沿着x轴寻找几个月,但是程序在这个错误上停止-

File "C:\Users\U321103\AppData\Local\Continuum\anaconda3\envs\Stats\lib\site- 
packages\pandas\plotting\_matplotlib\core.py", line 323, in _setup_subplots
fig = self.ax.get_figure()

AttributeError: 'str' object has no attribute 'get_figure'
这是我的绘图,在将“时间”列或月份指定给x轴后,它看起来与预期一样。谢谢大家!


在回答关于有问题的
ltycs.plot(ax='time')
行的注释后:

这一行
ax.xaxis.set\u major\u formatter(mdates.DateFormatter(“%Y-%m”)
实现了您所描述的功能(即在xaxis中显示月份)。绘制pandas.DataFrame时,
ax
参数需要matplotlib Axis对象,而不是字符串,这就是为什么会出现错误。包含有问题的行时得到正确标签的原因是-您没有运行
plt.minorticks\u on()
call

因此,您应该首先通过matplotlib子绘图创建matplotlib轴,然后打开minorticks_,然后打印pandas.DataFrames,将matplotlib ax对象传递给各自的绘图函数,如下所示:

#PRODUCE AND VISUALIZE FORECAST
pred_uc = results.get_forecast(steps=12-cm+1) # forecast steps in terms of "y" which is months
pred_ci = pred_uc.conf_int()
import matplotlib.dates as mdates
#from matplotlib.dates import MonthLocator
figure, ax = plt.subplots(figsize = (14,7))
plt.minorticks_on()
y['2020':].plot(label='observed', ax = ax)
pred_uc.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index,
                pred_ci.iloc[:, 0],
                pred_ci.iloc[:, 1], color='k', alpha=.25)
#ax.set_xlabel('Date')
ax.set_ylabel('MOS Wind Speed')
#add the LT monthly average to plot
lty = ylt.groupby(ylt.index.month).mean()
lty = lty.to_frame() 
lty.columns=['LT Mean']
ltyc = lty.iloc[0:12].reset_index() # extract curr month to end of LT mean monthly wind speed
#create date sequence using date format of df = y
ltyc['time'] = pd.to_datetime(ltyc["time"], format='%m').apply(lambda, 
dt:dt.replace(year=2020))#convert the "Date" col to yyyy-mm-dd 
ltycs = pd.Series(ltyc['LT Mean'].values, index=ltyc['time'])#convert to Series for plot
ltycs.plot(label='LT Mean',ax=ax,color='k')#ax='time' plots x months
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='g', linestyle='-', alpha=0.2)#alpha is the minor grid 
plt.legend()
plt.show()

你想用这条线来完成什么?
ltycs.plot(ax='time')
?嗨-这条线的目标是在x轴上绘制一年中的月份,这就是实际发生的情况。如果我把这句话注释掉,那么几个月的时间确实显示了这一点。然而,正如我上面所示,这条线上有一个错误,但它确实在x轴上绘制了月份。问题是代码到此为止,我没有展示更多的程序。谢谢你-我慢慢地从10多年的Matlab中获得python:)别担心,希望过渡会顺利:)
#PRODUCE AND VISUALIZE FORECAST
pred_uc = results.get_forecast(steps=12-cm+1) # forecast steps in terms of "y" which is months
pred_ci = pred_uc.conf_int()
import matplotlib.dates as mdates
#from matplotlib.dates import MonthLocator
figure, ax = plt.subplots(figsize = (14,7))
plt.minorticks_on()
y['2020':].plot(label='observed', ax = ax)
pred_uc.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index,
                pred_ci.iloc[:, 0],
                pred_ci.iloc[:, 1], color='k', alpha=.25)
#ax.set_xlabel('Date')
ax.set_ylabel('MOS Wind Speed')
#add the LT monthly average to plot
lty = ylt.groupby(ylt.index.month).mean()
lty = lty.to_frame() 
lty.columns=['LT Mean']
ltyc = lty.iloc[0:12].reset_index() # extract curr month to end of LT mean monthly wind speed
#create date sequence using date format of df = y
ltyc['time'] = pd.to_datetime(ltyc["time"], format='%m').apply(lambda, 
dt:dt.replace(year=2020))#convert the "Date" col to yyyy-mm-dd 
ltycs = pd.Series(ltyc['LT Mean'].values, index=ltyc['time'])#convert to Series for plot
ltycs.plot(label='LT Mean',ax=ax,color='k')#ax='time' plots x months
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='g', linestyle='-', alpha=0.2)#alpha is the minor grid 
plt.legend()
plt.show()