Jupyter(Python3)内核崩溃自动重启:绘图代码执行失败求助
Hey there, sorry to hear you're stuck with this kernel crash when trying to plot in Jupyter! That error message The kernel appears to have died. It will restart automatically is super frustrating, especially with such a simple plotting snippet. Let's go through some practical fixes that usually get things working again:
1. Verify & Update Matplotlib/Numpy Versions
Outdated dependencies are a common culprit here. First, check what versions you're running:
import matplotlib print(f"Matplotlib version: {matplotlib.__version__}") import numpy print(f"Numpy version: {numpy.__version__}")
If you're on a version older than Matplotlib 3.0 or Numpy 1.18, upgrade them:
- For pip environments:
pip install --upgrade matplotlib numpy - For conda environments:
conda update matplotlib numpy
2. Switch Matplotlib Backends
Matplotlib uses different "backends" to render plots, and sometimes the default one clashes with your Jupyter setup. Try these tweaks:
- First, run the magic command alone before importing libraries:
Then run your import and plotting code separately.%matplotlib inline - Alternatively, try the notebook backend instead:
%matplotlib notebook import matplotlib.pyplot as plt import numpy as np plt.plot(np.sin(np.linspace(0,2*np.pi, 100))) - If you just need to generate plots without interactive display, set a headless backend:
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np plt.plot(np.sin(np.linspace(0,2*np.pi, 100))) plt.savefig('test_plot.png') # Save instead of displaying inline
3. Ensure Kernel & Environment Match
Sometimes Jupyter is using a different Python kernel than the one where you installed Matplotlib/Numpy. Check this by running:
- On Linux/macOS:
!which python - On Windows:
!where python
Compare this path to the Python you used to install dependencies. If they don't match, reinstall the kernel for your correct environment:
python -m ipykernel install --user --name=your_environment_name
Then restart Jupyter and select this new kernel from the "Kernel > Change Kernel" menu.
4. Test with Minimal Code
Strip down your code to the absolute basics to rule out any hidden issues:
%matplotlib inline import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.show()
If this still crashes, the problem is with your environment, not the plotting logic.
5. Check System Resources
While your test code is tiny, sometimes low memory (RAM) can cause the kernel to crash unexpectedly. Close any unnecessary apps or browser tabs, then try running the code again.
6. Reinstall Jupyter Components
If all else fails, a fresh install of Jupyter's kernel components might fix the issue:
# Uninstall existing components pip uninstall -y ipykernel jupyter # Reinstall pip install ipykernel jupyter
Restart your Jupyter server after this and test again.
If none of these work, check the terminal where you launched Jupyter—there might be more detailed error messages there that can point to the root cause (like missing system libraries for Matplotlib).
内容的提问来源于stack exchange,提问作者R. Pellerej

