Jupyter Notebook中Matplotlib绘图显示尺寸减半问题求助
Great question—this is a super common issue, especially on high-DPI screens, and it’s definitely a known pain point (though not exactly a "bug" per se, more of an alignment gap between Matplotlib’s configuration and Jupyter’s rendering layer).
Why This Happens
Matplotlib’s figure.figsize is defined in inches, but Jupyter Notebook applies a default display scaling factor (often 2.0 for high-DPI displays) that shrinks the rendered plot to half its intended size. When you double the figsize manually, the plot gets back to the right dimensions, but the text elements are rendered based on the original DPI settings—so they end up looking tiny relative to the larger plot.
Permanent Solutions
Here are a few non-temporary fixes to get your plots displaying correctly with properly sized text:
1. Adjust Matplotlib’s Global DPI Setting
You can set this once at the start of your Notebook (or add it to your global matplotlibrc config file for all sessions):
from matplotlib import pyplot as plt # For high-DPI screens, set dpi to 200; use 100 for standard displays plt.rcParams['figure.dpi'] = 200
This matches Matplotlib’s rendering DPI to Jupyter’s display scaling. Now your figsize=[6.0, 4.0] will display at the full intended size, and text will scale proportionally with the plot.
2. Use Jupyter’s Magic Commands for Inline Backend
Run these lines at the top of your Notebook (you can even add them to your Jupyter startup script to auto-apply every session):
%config InlineBackend.figure_format = 'retina' %config InlineBackend.dpi = 200
The retina format optimizes rendering for high-DPI screens, and setting the inline backend DPI ensures the plot dimensions and text size align with what you see in the browser.
3. Tweak Jupyter’s Global Configuration
If you want a system-wide fix, edit your jupyter_notebook_config.py file (generate it with jupyter notebook --generate-config if you haven’t already) and add:
# Adjust display zoom to match Matplotlib's default scaling c.NotebookApp.webapp_settings['zoom_level'] = 1.0
Note that this can be affected by your browser’s zoom settings, so the first two methods are usually more reliable.
How to Verify
After applying any of these fixes, run your test code to confirm:
from matplotlib import pyplot print(pyplot.rcParams['figure.figsize'], pyplot.rcParams['figure.dpi'])
Then plot a simple test to check dimensions and text size:
plt.plot([1, 2, 3], [4, 5, 6]) plt.title("Test Plot") plt.show()
The displayed plot should match your figsize settings, and the title/axes text should look properly sized.
内容的提问来源于stack exchange,提问作者Adam

