How do I set matplotlib's DPI for every cell in Jupyter

时光总嘲笑我的痴心妄想 提交于 2019-12-20 05:31:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!