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

Matplotlib中rcParams忽略特定默认FigureSize的问题求助

Fixing Matplotlib rcParams Figure Size Not Applying Consistently

Let's break down why your (15, 9.3) default figure size is being ignored while extreme values work, and walk through actionable fixes.

Common Causes & Solutions

1. Timing of rcParams Configuration

Matplotlib loads its default settings when you first import pyplot. If you set rcParams after importing plt, initial setup might already have locked in a default size. Fix this by setting parameters before importing pyplot, or explicitly update them immediately after:

# Option 1: Set before importing pyplot
import matplotlib
matplotlib.rcParams['figure.figsize'] = (15, 9.3)
import matplotlib.pyplot as plt

# Option 2: Update right after importing (no plotting code runs first)
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.figsize': (15, 9.3)})

Verify it works by checking the figure size right after creating a plot:

plt.plot([1,2,3])
print(plt.gcf().get_size_inches())  # Should return (15.0, 9.3)
plt.show()

2. GUI Backend Auto-Adjustment

Interactive backends like TkAgg or QtAgg often adjust window sizes based on screen resolution or OS display scaling (e.g., Windows 125% scaling). This can override moderate figsize values but won’t constrain extreme sizes.

  • Test with a non-interactive backend to rule out GUI interference:
    import matplotlib
    matplotlib.use('Agg')  # Saves to file instead of opening a window
    matplotlib.rcParams['figure.figsize'] = (15, 9.3)
    import matplotlib.pyplot as plt
    
    plt.plot([1,2,3])
    plt.savefig('test_figure.png')
    # Check dimensions: 15*100 = 1500px wide, 9.3*100 = 930px tall (default dpi=100)
    
  • If the non-interactive test works, lock DPI to avoid scaling:
    plt.rcParams.update({
        'figure.figsize': (15, 9.3),
        'figure.dpi': 100,  # Fix DPI to match your desired physical size
        'figure.autolayout': False
    })
    

3. Third-Party Library Overrides

Libraries like Seaborn or Pandas often modify Matplotlib’s defaults when you initialize their themes. If you use these tools, set your rcParams after their setup code:

import seaborn as sns
sns.set_theme()  # This may override figsize
plt.rcParams['figure.figsize'] = (15, 9.3)  # Re-apply your default

4. System Display Scaling

OS display scaling (e.g., 125% on Windows, 1.2x on macOS) can make Matplotlib adjust figsize for readability. To bypass this, explicitly set the figure’s DPI to match your screen’s physical DPI:

import matplotlib.pyplot as plt

# Get screen DPI (example for TkAgg backend)
fig_manager = plt.get_current_fig_manager()
screen_dpi = fig_manager.window.winfo_screendpi()

plt.rcParams.update({
    'figure.figsize': (15, 9.3),
    'figure.dpi': screen_dpi
})

Final Troubleshooting Step

If nothing else works, reset all Matplotlib settings to default before applying your figsize to eliminate conflicting configurations:

import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)  # Reset to factory defaults
mpl.rcParams['figure.figsize'] = (15, 9.3)
import matplotlib.pyplot as plt

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:11:21