问题
When I try to set the DPI for Matplotlib plots in Jupyter, it appears to be reset in every cell:
The code:
# In[1]:
get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib import pyplot
print(pyplot.rcParams['figure.dpi'])
pyplot.rcParams['figure.dpi'] = 150
print(pyplot.rcParams['figure.dpi'])
# In[2]:
print(pyplot.rcParams['figure.dpi'])
How do I set a consistent DPI for plots throughout the entire notebook?
This is using Jupyter 1.0.0, Matplotlib 3.0.1, Python 3.6 on Windows 10.
回答1:
That's a bug[*] in IPython. To work around that use the first cell of your notebook to set the backend. Manipulate the rcParams in subsequent cells.
[*] See:
- ipython/ipython#11098
- matplotlib/matplotlib#11693
- matplotlib/matplotlib#11393
- matplotlib/matplotlib#11815
- jupyter/notebook#3385
Let me quote here a comment by @takluyver:
There's a bit of setup that happens just after the cell where %matplotlib inline is called, I think. So if you set things in that cell, they can be overridden by IPython's setup. After that, things you change should (I hope) be kept between cells.
来源:https://stackoverflow.com/questions/53328248/how-do-i-set-matplotlibs-dpi-for-every-cell-in-jupyter