Matplotlib子图报错:'Axes'对象无'is_first_col'属性
AttributeError: 'Axes' object has no attribute 'is_first_col' 解决方法
问题重现
运行以下代码时触发AttributeError:
import matplotlib.pyplot as plt fig, ax = plt.subplots(1,2) import pandas as pd df = pd.DataFrame([1,2,3]) df.plot(ax=ax[0])
环境版本
- matplotlib: '3.8.4'
- pandas: '1.2.0'
- python: 3.11.0
报错信息
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[3], line 5 3 import pandas as pd 4 df = pd.DataFrame([1,2,3]) ----> 5 df.plot(ax=ax[0]) File ~/miniforge3/envs/ExMAS/lib/python3.11/site-packages/pandas/plotting/_core.py:955, in PlotAccessor.__call__(self, *args, **kwargs) 952 label_name = label_kw or data.columns 953 data.columns = label_name ---> 955 return plot_backend.plot(data, kind=kind, **kwargs) File ~/miniforge3/envs/ExMAS/lib/python3.11/site-packages/pandas/plotting/_matplotlib/__init__.py:61, 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 File ~/miniforge3/envs/ExMAS/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py:283, in MPLPlot.generate(self) 281 self._add_table() 282 self._make_legend() ---> 283 self._adorn_subplots() 285 for ax in self.axes: 286 self._post_plot_logic_common(ax, self.data) File ~/miniforge3/envs/ExMAS/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py:485, in MPLPlot._adorn_subplots(self) 483 all_axes = self._get_subplots() 484 nrows, ncols = self._get_axes_layout() ---> 485 handle_shared_axes( 486 axarr=all_axes, 487 nplots=len(all_axes), 488 naxes=nrows * ncols, 489 nrows=nrows, 490 ncols=ncols, 491 sharex=self.sharex, 492 sharey=self.sharey, 493 ) 495 for ax in self.axes: 496 if self.yticks is not None: File ~/miniforge3/envs/ExMAS/lib/python3.11/site-packages/pandas/plotting/_matplotlib/tools.py:400, in handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey) 395 if ncols > 1: 396 for ax in axarr: 397 # only the first column should get y labels -> set all other to 398 # off as we only have labels in the first column and we always 399 # have a subplot there, we can skip the layout test ---> 400 if ax.is_first_col(): 401 continue 402 if sharey or _has_externally_shared_axis(ax, "y"): AttributeError: 'Axes' object has no attribute 'is_first_col'
问题原因
pandas 1.2.0的绘图代码依赖Matplotlib Axes对象的is_first_col()方法,但该方法在Matplotlib 3.8.4中已被移除,属于版本兼容冲突。
解决方案
方案1:升级pandas版本
将pandas升级到与Matplotlib 3.8.4兼容的版本(建议升级到1.5.x及以上,或最新稳定版),执行命令:
pip install --upgrade pandas
方案2:降级Matplotlib版本
将Matplotlib降级到pandas 1.2.0支持的版本(建议降级到3.3.x及以下),执行命令:
pip install matplotlib==3.3.4
方案3:临时绕过(不推荐生产环境使用)
手动给Axes对象添加is_first_col()方法,在调用df.plot()前插入以下代码:
def mock_is_first_col(self): return self.get_subplotspec().is_first_col() import matplotlib.axes matplotlib.axes.Axes.is_first_col = mock_is_first_col
内容的提问来源于stack exchange,提问作者Intelligent-Infrastructure
相关产品推荐
相关产品推荐

