Spyder绘图面板中Matplotlib的figsize参数无可见效果的原因及解决方法
figsize Doesn't Change Plot Size in Spyder & How to Fix It Great question! Let's break down exactly why your figsize settings aren't having the effect you expect in Spyder, and walk through solutions to make your plots display at the size you want.
Why Your figsize Isn't Working
The core issue here is Spyder's default plot backend behavior:
- By default, Spyder uses the "Inline" graphics backend, which renders plots as static images embedded directly in the IPython Console's plot pane.
- This backend automatically scales all plots to fit the pane's current size, ignoring your
figsizeparameter for display purposes (the underlying figure file still uses yourfigsizeif you save it withplt.savefig()). - Your code itself isn't the problem—your
plt.figure(figsize=(16,16))call is correctly creating a large figure; Spyder's just shrinking it to fit the panel.
Fixes to Make Plots Show Larger in Spyder
1. Switch to a Standalone Graphics Backend
The most straightforward fix is to change Spyder's backend to one that opens plots in independent windows (where figsize works as expected):
- Go to
Tools > Preferencesin Spyder's menu bar. - Navigate to
IPython Console > Graphics. - Under Graphics backend, change the dropdown from "Inline" to "Automatic", "Qt5", or "Qt6".
- Click "Apply" and restart Spyder (or restart the IPython kernel).
Now when you run your code, plots will pop up in a separate resizable window, and your figsize=(16,16) setting will be fully respected.
2. Adjust DPI Alongside figsize (Optional)
If you want even more control over the physical size of your plot, combine figsize with the dpi parameter (dots per inch):
py.figure(figsize=(16,16), dpi=120) # Higher DPI = sharper, larger window
This increases the pixel density of your figure, making the window larger and the plot details crisper.
3. Optimize Your Interactive Plotting Code
Your current code clears axes (py.cla()) every loop iteration, which can cause unnecessary redraws. A more efficient approach is to create your axes and plot objects once, then update their data in the loop. This also ensures your figsize setting stays consistent:
import numpy as np from matplotlib import pyplot as py L=2 nx=130 dx=L/(nx-1) nt=25 CFL=1 u= np.ones(nx) u[int(0.5/dx):int(1/dx+1)]=2 x=np.linspace(0,L,nx) py.style.use('classic') dt=(CFL*dx) / (max(u)) nx2 = 41 dx2 = 2 / (nx2 - 1) nt2 = 20 nu2 = 0.3 sigma2 = 0.2 dt2 = sigma2 * dx2**2 / nu2 u2 = np.ones(nx2) u2[int(0.5 / dx2):int(1 / dx2 + 1)] = 2 x2 = np.linspace(0, 2, nx2) # Create figure and axes ONCE outside the loop fig, (ax1, ax2) = py.subplots(2, 1, figsize=(16,16)) line1, = ax1.plot(x, u, 'ko-') line2, = ax2.plot(x2, u2, color='black') # Set axis limits and labels once ax1.set_ylim(0, 2.2) ax1.set_title("Inviscid 1D Burger's Equation") ax1.set_xlabel("x") ax1.set_ylabel("u") ax2.set_ylim(0, 2.2) ax2.set_title('Diffusion') ax2.set_xlabel("x") ax2.set_ylabel("u") fig.tight_layout() for i in range(nt): un = u.copy() un2 = u2.copy() u[1:nx] = un[1:nx] - ((un[1:nx]*dt)/(1*dx))*(un[1:nx]-un[0:nx-1]) # Update line data instead of clearing axes line1.set_data(x, u) u2[1:nx2-1] = un2[1:nx2-1] + (nu2 * dt2 / dx2**2) * ( un2[2:nx2] - 2 * un2[1:nx2-1] + un2[:nx2-2] ) line2.set_data(x2, u2) py.pause(0.0001)
This code runs smoother and ensures your figure size remains consistent throughout the animation.
4. Save the Figure to Verify figsize Works
If you want to confirm your figsize is being applied correctly, save the figure to a file:
py.savefig("my_large_plot.png")
When you open this file externally, it will be the exact size you set with figsize, proving your code is working—Spyder's just scaling the display in its pane.
内容的提问来源于stack exchange,提问作者Hamza Hussian

