能否永久修改Jupyter Notebook中Matplotlib内嵌绘图的默认样式与尺寸?
Make Matplotlib Settings Permanent in Jupyter Notebook
Great question—having to retype those setup lines every time you open a Notebook is such a hassle. Here are two reliable ways to lock in your preferred Matplotlib style and figure size permanently:
Method 1: Modify Matplotlib's Global Configuration File
This will apply your settings across all Matplotlib instances (not just Jupyter), so it’s perfect if you want consistency everywhere.
- First, find where your
matplotlibrcconfig file lives. Run this in a Notebook cell:import matplotlib print(matplotlib.matplotlib_fname()) - Open that file in a text editor. Look for these lines and update them:
- Replace the default
figure.figsizewith:figure.figsize: 16, 8 - Add or update the style line to use ggplot:
style: ggplot
- Replace the default
- Save the file. Next time you launch Jupyter or use Matplotlib anywhere, these settings will be active by default.
Method 2: Use an IPython Startup Script
If you only want these settings to apply in Jupyter/IPython (and leave regular Python scripts untouched), this is the way to go.
- First, create an IPython profile if you don’t have one already. Open your terminal and run:
ipython profile create - Navigate to the startup folder of your default profile. On most systems, this is:
~/.ipython/profile_default/startup/ - Create a new Python file here (name it something like
00-matplotlib-setup.py—the00-ensures it runs first). Paste this code inside:import matplotlib.pyplot as plt from IPython import get_ipython # Enable inline plotting for Jupyter get_ipython().run_line_magic('matplotlib', 'inline') # Set default figure size plt.rcParams['figure.figsize'] = [16, 8] # Apply ggplot style plt.style.use('ggplot') - Save the file. Now every time you start a Jupyter Notebook session, this script will run automatically, applying your settings without you lifting a finger.
Either method works perfectly—pick the one that fits your workflow best!
内容的提问来源于stack exchange,提问作者agftrading
相关产品推荐
相关产品推荐

