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

Jupyter Notebook导入matplotlib.pyplot(3.2.1)报错求助,环境冲突

Hey there, let's tackle this matplotlib headache you're dealing with in Jupyter Notebook—it’s almost certainly an environment conflict caused by ArcGIS’s bundled Python 2.7 (and its old matplotlib 1.x version) clashing with your Anaconda 3 setup. Let’s break down what’s going on and fix it step by step:

Problem Breakdown

The core issue here is that Jupyter isn’t using your Anaconda 3 environment (where you have matplotlib 3.2.1 installed) — instead, it’s accidentally pulling in ArcGIS’s outdated Python 2.7 and its matching matplotlib 1.x. That’s why:

  • import matplotlib works (since the old version exists), but calling matplotlib.pyplot or pandas plotting functions throws errors (the 1.x version lacks newer modules like matplotlib.artist and attributes like rcParamsOrig).
  • PyCharm works fine because you’ve configured it to use the correct Anaconda environment directly.
Step-by-Step Fixes

1. Verify Which Python Environment Jupyter Is Using

First, confirm the problem by running this in a Jupyter cell:

import sys
print(sys.executable)

If the output points to ArcGIS’s Python 2.7 directory (e.g., C:\Program Files (x86)\ArcGIS\Desktop10.x\python.exe), we’ve found the culprit.

2. Add Your Anaconda Environment to Jupyter’s Kernels

Jupyter might not be aware of your Anaconda environment yet. Let’s fix that:

  1. Open the Anaconda Prompt (make sure it’s using your target Python 3 environment — e.g., base or a custom virtual environment).
  2. Install the ipykernel package (needed to connect environments to Jupyter):
    pip install ipykernel
    
  3. Register your Anaconda environment as a Jupyter kernel:
    python -m ipykernel install --user --name=your_anaconda_env --display-name="Python 3 (Anaconda)"
    
    Replace your_anaconda_env with your actual environment name (run conda env list in Anaconda Prompt to see all environments if you’re unsure).

3. Switch to the Correct Kernel in Jupyter

Restart Jupyter Notebook, then:

  • Go to the Kernel menu in the top bar
  • Select Change kernel → Pick the Python 3 (Anaconda) kernel you just added

If ArcGIS’s Python is still interfering, adjust your system’s environment variables to prioritize Anaconda:

  1. Right-click This PC → Properties → Advanced system settings → Environment Variables
  2. In the System variables section, find the Path variable
  3. Move your Anaconda paths (e.g., C:\Users\YourName\Anaconda3 and C:\Users\YourName\Anaconda3\Scripts) above any ArcGIS Python paths
  4. Restart your computer for changes to take effect

5. Reinstall Matplotlib in the Correct Environment

Double-check that matplotlib 3.2.1 is installed in your Anaconda environment. Run this in Anaconda Prompt:

pip uninstall matplotlib -y
pip install matplotlib==3.2.1
Test the Fix

Once you’ve switched to the correct kernel, run this test code in Jupyter:

import matplotlib
print(matplotlib.__version__)  # Should output 3.2.1
import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4]
y = [2, 2, 2, 2, 2]
plt.plot(x, y)
plt.show()

If a simple line plot pops up, you’re good to go! For your pandas histogram test, this should now work too:

import pandas as pd
x = [0, 1, 2, 3, 4]
y = [2, 2, 2, 2, 2]
df = pd.DataFrame([x,y], columns=['a','b','c','d','f'])
df['b'].hist()
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 16:42:54