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

能否永久修改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.

  1. First, find where your matplotlibrc config file lives. Run this in a Notebook cell:
    import matplotlib
    print(matplotlib.matplotlib_fname())
    
  2. Open that file in a text editor. Look for these lines and update them:
    • Replace the default figure.figsize with:
      figure.figsize: 16, 8
      
    • Add or update the style line to use ggplot:
      style: ggplot
      
  3. 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.

  1. First, create an IPython profile if you don’t have one already. Open your terminal and run:
    ipython profile create
    
  2. Navigate to the startup folder of your default profile. On most systems, this is:
    ~/.ipython/profile_default/startup/
    
  3. Create a new Python file here (name it something like 00-matplotlib-setup.py—the 00- 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')
    
  4. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:24:33