我正在尝试使用熊猫绘图库中的df.plot
进行绘图,并使用以下代码:
df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
sharey=True, ylabel = "Percent Change", title="Average movement")
我认为这可能与使用np.transpose()
有关,因为它会将其转换为numpy数组,但转换回pd.DataFrame()
后,错误仍然存在。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-269-85f6c80ca026> in <module>
1 df_mean = pd.DataFrame(df_mean)
2
----> 3 df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
4 title="Average movement",
5 sharey=True, ylabel = "Percent Change")
~anaconda3libsite-packagespandasplotting_core.py in __call__(self, *args,
**kwargs)
845 keyword_args = ", ".join(
846 f"{name}={repr(value)}" for (name, default), value in
zip(arg_def, args)
--> 847 )
848 msg = (
849 "`Series.plot()` should not be called with positional "
~anaconda3libsite-packagespandasplotting_matplotlib__init__.py in plot(data,
kind, **kwargs)
59 kwargs["ax"] = getattr(ax, "left_ax", ax)
60 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 61 plot_obj.generate()
62 plot_obj.draw()
63 return plot_obj.result
~anaconda3libsite-packagespandasplotting_matplotlibcore.py in generate(self)
261 else:
262 return self.data.shape[1]
--> 263
264 def draw(self):
265 self.plt.draw_if_interactive()
~anaconda3libsite-packagespandasplotting_matplotlibcore.py in
_make_plot(self)
1075 self.data = self.data.fillna(value=0)
1076 self.x_compat = plot_params["x_compat"]
-> 1077 if "x_compat" in self.kwds:
1078 self.x_compat = bool(self.kwds.pop("x_compat"))
1079
~anaconda3libsite-packagespandasplotting_matplotlibcore.py in _plot(cls, ax,
x, y, style, column_num, stacking_id, **kwds)
1102
1103 stacking_id = self._get_stacking_id()
-> 1104 is_errorbar = com.any_not_none(*self.errors.values())
1105
1106 colors = self._get_colors()
~anaconda3libsite-packagespandasplotting_matplotlibconverter.py in
wrapper(*args, **kwargs)
64 with pandas_converters():
65 return func(*args, **kwargs)
---> 66
67 return wrapper
68
~anaconda3libsite-packagespandasplotting_matplotlibcore.py in _plot(cls, ax,
x, y, style, is_errorbar, **kwds)
654
655 if is_errorbar:
--> 656 if "xerr" in kwds:
657 kwds["xerr"] = np.array(kwds.get("xerr"))
658 if "yerr" in kwds:
~anaconda3libsite-packagesmatplotlibaxes_axes.py in plot(self, scalex,
scaley, data, *args, **kwargs)
1741
1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)]
1744 for line in lines:
1745 self.add_line(line)
~anaconda3libsite-packagesmatplotlibaxes_base.py in __call__(self, data,
*args, **kwargs)
271 this += args[0],
272 args = args[1:]
--> 273 yield from self._plot_args(this, kwargs)
274
275 def get_next_color(self):
~anaconda3libsite-packagesmatplotlibaxes_base.py in _plot_args(self, tup,
kwargs)
416 if ncx > 1 and ncy > 1 and ncx != ncy:
417 raise ValueError(f"x has {ncx} columns but y has {ncy}
columns")
--> 418 return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
419 for j in range(max(ncx, ncy))]
420
~anaconda3libsite-packagesmatplotlibaxes_base.py in <listcomp>(.0)
416 if ncx > 1 and ncy > 1 and ncx != ncy:
417 raise ValueError(f"x has {ncx} columns but y has {ncy}
columns")
--> 418 return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
419 for j in range(max(ncx, ncy))]
420
~anaconda3libsite-packagesmatplotlibaxes_base.py in _makeline(self, x, y,
kw, kwargs)
310 default_dict = self._getdefaults(set(), kw)
311 self._setdefaults(default_dict, kw)
--> 312 seg = mlines.Line2D(x, y, **kw)
313 return seg
314
~anaconda3libsite-packagesmatplotliblines.py in __init__(self, xdata, ydata,
linewidth, linestyle, color, marker, markersize, markeredgewidth, markeredgecolor,
markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle,
solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery,
**kwargs)
388 # update kwargs before updating data to give the caller a
389 # chance to init axes (and hence unit support)
--> 390 self.update(kwargs)
391 self.pickradius = pickradius
392 self.ind_offset = 0
~anaconda3libsite-packagesmatplotlibartist.py in update(self, props)
994 func = getattr(self, f"set_{k}", None)
995 if not callable(func):
--> 996 raise AttributeError(f"{type(self).__name__!r}
object "
997 f"has no property {k!r}")
998 ret.append(func(v))
AttributeError: 'Line2D' object has no property 'ylabel'
我可以在我的mac上运行这段代码,但是当我把它转移到我的桌面时,我得到了这个错误,我不知道为什么。我以为可能是版本问题,但我更新了pandas,没有解决任何问题。
有人知道是什么导致了这样的事情吗?
你可以试试这个技巧:
如果ylabel
参数有问题,请将其移除,直接设置为ax
。
ax = df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
sharey=True, title="Average movement")
ax.set_ylabel('Percent Change')
plt.show()