You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pandas读取CSV后,matplotlib的plt.savefig/show报NoneType错误

问题分析:pandas读取CSV后Matplotlib绘图报'NoneType' object is not callable错误

问题场景

使用pandas的read_csv读取CSV文件后,即使不使用文件数据,仅用Matplotlib绘制简单图形也会报错'NoneType' object is not callable。该报错仅出现在Visual Studio中,IDLE可正常运行;若将绘图代码移至pd.read_csv之前则无问题。

复现代码

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data.csv")

time =  [0,10,20]
vel = [0,1,2]
fig,axes = plt.subplots()
axes.plot(time,vel)
plt.show()
plt.savefig(fname = "pic.png" )

报错堆栈

Traceback:
Traceback (most recent call last):
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\numpy\core\getlimits.py", line 384, in __new__
    dtype = numeric.dtype(dtype)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qt.py", line 455, in _draw_idle
    self.draw()
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\figure.py", line 2803, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\axes\_base.py", line 3020, in draw
    self._unstale_viewLim()
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\axes\_base.py", line 776, in _unstale_viewLim
    self.autoscale_view(**{f"scale{name}": scale
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\axes\_base.py", line 2932, in autoscale_view
    handle_single_axis(
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\axes\_base.py", line 2895, in handle_single_axis
    x0, x1 = locator.nonsingular(x0, x1)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\ticker.py", line 1654, in nonsingular
    return mtransforms.nonsingular(v0, v1, expander=.05)
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\matplotlib\transforms.py", line 2880, in nonsingular
    if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
  File "C:\Users\user1\AppData\Roaming\Python\Python310\site-packages\numpy\core\getlimits.py", line 387, in __new__
    dtype = numeric.dtype(type(dtype))
TypeError: 'NoneType' object is not callable

原因分析

从报错堆栈可知,问题根源是numpy的numeric.dtype被意外置为None。结合场景细节,核心原因是:

  • pandas在导入过程中会对numpy的内部模块进行初始化或修改,导致numpy的numeric模块中dtype属性的引用被破坏
  • Visual Studio的Python运行环境(如Qt绘图后端、模块加载机制)与IDLE存在差异,触发了这个被破坏的引用问题
  • 绘图代码放在pd.read_csv之前时,numpy模块尚未被pandas修改,因此能正常运行

解决办法

1. 调整模块导入顺序

先导入matplotlib相关模块,再导入pandas,避免pandas修改numpy模块后影响matplotlib的初始化。同时注意plt.savefig要放在plt.show之前(plt.show()会关闭图形对象,后续无法保存有效内容):

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("data.csv")

time =  [0,10,20]
vel = [0,1,2]
fig,axes = plt.subplots()
axes.plot(time,vel)
plt.savefig(fname = "pic.png" )
plt.show()

2. 统一库版本兼容性

该问题大概率是numpy、pandas、matplotlib之间的版本不兼容导致,建议:

  • 升级到最新稳定版:pip install --upgrade numpy pandas matplotlib
  • 若最新版仍有问题,尝试降级到已知兼容的版本组合,例如numpy=1.23.x、pandas=1.5.x、matplotlib=3.6.x

3. 检查Visual Studio环境

确保VS使用的Python解释器与IDLE一致,避免环境差异引发问题:在VS的Python环境选择器中切换到IDLE使用的解释器即可。

4. 排查模块命名冲突

检查项目中是否存在重命名为numeric.py、dtype.py的自定义文件,此类文件会覆盖numpy的内部模块,导致引用异常。


内容的提问来源于stack exchange,提问作者Hooke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:01:35