【发布时间】:2021-09-09 15:37:18
【问题描述】:
我正在尝试使用 pandas 绘图库中的 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")
~\anaconda3\lib\site-packages\pandas\plotting\_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 "
~\anaconda3\lib\site-packages\pandas\plotting\_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
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in generate(self)
261 else:
262 return self.data.shape[1]
--> 263
264 def draw(self):
265 self.plt.draw_if_interactive()
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.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
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.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()
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\converter.py in
wrapper(*args, **kwargs)
64 with pandas_converters():
65 return func(*args, **kwargs)
---> 66
67 return wrapper
68
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.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:
~\anaconda3\lib\site-packages\matplotlib\axes\_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)
~\anaconda3\lib\site-packages\matplotlib\axes\_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):
~\anaconda3\lib\site-packages\matplotlib\axes\_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
~\anaconda3\lib\site-packages\matplotlib\axes\_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
~\anaconda3\lib\site-packages\matplotlib\axes\_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
~\anaconda3\lib\site-packages\matplotlib\lines.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
~\anaconda3\lib\site-packages\matplotlib\artist.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只能从pandas 1.1.0获得。kind='line'也是默认的,所以你不需要包含它。检查pd.__version__ -
添加到@TrentonMcKinney 评论关于检查
pd.__version__。从您的脚本/笔记本运行时,这将打印引发此错误的pandas的版本。您可能更新了系统的pandas版本,而不是 anaconda 的版本。 -
@Jason 我想就是这样。我不知道我也需要用它来更新 Anaconda,所以感谢您指出这一点!全部修复:)
-
@Falcon 我很高兴你设法解决了你的问题。 system 和 anaconda 包是相互独立的。因此,将来您只需要更新您的 conda 环境(如果这是您运行脚本/笔记本的地方)。
which python将向您显示您正在使用的 python 所在的路径。如果您搜索“python 虚拟环境”,它会解释我无法放入评论的内容。 -
所以在 anaconda 提示符下,执行
conda update -all
标签: python-3.x pandas matplotlib anaconda