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:
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 matplotlibworks (since the old version exists), but callingmatplotlib.pyplotor pandas plotting functions throws errors (the 1.x version lacks newer modules likematplotlib.artistand attributes likercParamsOrig).- PyCharm works fine because you’ve configured it to use the correct Anaconda environment directly.
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:
- Open the Anaconda Prompt (make sure it’s using your target Python 3 environment — e.g.,
baseor a custom virtual environment). - Install the
ipykernelpackage (needed to connect environments to Jupyter):pip install ipykernel - Register your Anaconda environment as a Jupyter kernel:
Replacepython -m ipykernel install --user --name=your_anaconda_env --display-name="Python 3 (Anaconda)"your_anaconda_envwith your actual environment name (runconda env listin 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
4. (Optional but Recommended) Fix Environment Variable Priority
If ArcGIS’s Python is still interfering, adjust your system’s environment variables to prioritize Anaconda:
- Right-click This PC → Properties → Advanced system settings → Environment Variables
- In the System variables section, find the
Pathvariable - Move your Anaconda paths (e.g.,
C:\Users\YourName\Anaconda3andC:\Users\YourName\Anaconda3\Scripts) above any ArcGIS Python paths - 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
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

