Fedora 36下使用pandas绘图出现报错问题求助
pandas 1.5.x中plot方法subplots参数报错的解决
问题背景
我在RHEL8及更早版本使用该工具一直正常,但同事在Fedora 36(安装了新版本pandas/matplotlib)运行时出现报错。
环境信息
matplotlib 3.6.1 /home/loberman/.local/lib/python3.10/site-packages pip pandas 1.5.1 /home/loberman/.local/lib/python3.10/site-packages pip
出错代码
cpu_mem_data = read_csv(fname_mem, sep=' ', index_col=1, parse_dates=True) cpumain = cpu_mem_data[['CPU-Wait%','CPU-User%','CPU-Nice%','CPU-Sys%',]] cpumain.plot(grid='on',subplots='True',style=['r','g','b','y'],sharex=True,kind='area',title=plot_title+' cpu-view')
这段代码已经稳定运行近10年,从未出现过问题。
报错信息
File "/home/loberman/collectl-parser/./parse-collectl-python3.py", line 264, in <module> cpumain.plot(grid='on',subplots='True',style=['r','g','b','y'],sharex=True,kind='area',title=plot_title+' cpu-view') File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_core.py", line 1000, in __call__ return plot_backend.plot(data, kind=kind, **kwargs) File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_matplotlib/__init__.py", line 70, in plot plot_obj = PLOT_CLASSES[kind](data, **kwargs) File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1530, in __init__ LinePlot.__init__(self, data, **kwargs) File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1337, in __init__ MPLPlot.__init__(self, data, **kwargs) File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 187, in __init__ self.subplots = self._validate_subplots_kwarg(subplots) File "/home/loberman/.local/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 352, in _validate_subplots_kwarg raise ValueError( ValueError: When subplots is an iterable, each entry should be a list/tuple of column names.
临时解决方法
将pandas降级至1.4版本后,问题恢复正常,降级操作日志如下:
Defaulting to user installation because normal site-packages is not writeable Collecting pandas==1.4 Downloading pandas-1.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB) |████████████████████████████████| 11.7 MB 2.7 MB/s Requirement already satisfied: python-dateutil>=2.8.1 in /usr/lib/python3.10/site-packages (from pandas==1.4) (2.8.1) Requirement already satisfied: pytz>=2020.1 in /home/loberman/.local/lib/python3.10/site-packages (from pandas==1.4) (2022.5) Requirement already satisfied: numpy>=1.21.0 in /home/loberman/.local/lib/python3.10/site-packages (from pandas==1.4) (1.23.4) Requirement already satisfied: six>=1.5 in /usr/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas==1.4) (1.16.0) Installing collected packages: pandas Attempting uninstall: pandas Found existing installation: pandas 1.5.1 Uninstalling pandas-1.5.1: Successfully uninstalled pandas-1.5.1 Successfully installed pandas-1.4.0
问题根源与永久修复
pandas 1.5.x对subplots参数的校验逻辑做了严格调整:旧版本中字符串格式的'True'会被自动解析为布尔值True,但新版本会把字符串视为可迭代对象,触发了针对迭代类型参数的校验规则,从而抛出错误。
不需要降级pandas,只需要把代码中的subplots='True'修改为布尔值subplots=True即可彻底解决问题,修改后的代码如下:
cpumain.plot(grid='on',subplots=True,style=['r','g','b','y'],sharex=True,kind='area',title=plot_title+' cpu-view')
内容的提问来源于stack exchange,提问作者Laurence Oberman
相关产品推荐
相关产品推荐

