Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weirdness with inline figure DPI settings in Jupyter Notebook #9217

Closed
tui-rob opened this issue Sep 22, 2017 · 17 comments
Closed

Weirdness with inline figure DPI settings in Jupyter Notebook #9217

tui-rob opened this issue Sep 22, 2017 · 17 comments

Comments

@tui-rob
Copy link

tui-rob commented Sep 22, 2017

I find the default inline plotting DPI (72) of figures in Jupyter too small and would like to plot inline figures at 100 by default. However, the (i) import of pyplot and (ii) use of %matplotlib inline both seem to override the figure.dpi setting of 100 I have put in my .matplotlibrc file. I can address this issue by using mpl.rcParams['figure.dpi'] = 100 after importing pyplot and running %matplotlib inline, but think it would be better if this was not required every time.

I'm not sure if this is an issue with matplotlib, Jupyter Notebook and/or ipykernel.

Any assistance appreciated.

Issue 1: Import of pyplot overrides figure.dpi in .matplotlibrc
001

Issue 2: Use of %matplotlib inline magic overrides figure.dpi in .matplotlibrc
002

Matplotlib version

  • Operating System: Win32
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6
  • Jupyter Version : Notebook 5.0, Jupyter core 4.3
  • iPython: 5.3, ipykernel: 4.6.1
@jklymak
Copy link
Member

jklymak commented Sep 22, 2017

Any reason to not use %matplotlib notebook?

@tacaswell
Copy link
Member

attn @Carreau

@Carreau
Copy link
Contributor

Carreau commented Sep 22, 2017

Yes, the inline backend has different rcParam (historically) we should likely revisit those now that there is matplotlib 2; Screen resolution has also evolved quite a bit since we added that.

I guess the "Correct" way to do it would be to have a notebook style, and activate that style when doing inline.

rc = Dict({'figure.figsize': (6.0,4.0),
        # play nicely with white background in the Qt and notebook frontend
        'figure.facecolor': (1,1,1,0),
        'figure.edgecolor': (1,1,1,0),
        # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
        'font.size': 10,
        # 72 dpi matches SVG/qtconsole
        # this only affects PNG export, as SVG has no dpi setting
        'figure.dpi': 72,
        # 10pt still needs a little more room on the xlabel:
        'figure.subplot.bottom' : .125
        },
        help="""Subset of matplotlib rcParams that should be different for the
        inline backend."""
    ).tag(config=True)

I'm pretty sure there is already an issue with that, but I could not find it, so I opened ipython/ipykernel#267

@tui-rob
Copy link
Author

tui-rob commented Sep 26, 2017

Thanks for looking into this. I can see why the IPython inline backend rc param over-ride is causing the second part of my original issue. Per this SO thread, this can be addressed by setting c.InlineBackend.rc to an empty dict in ~/.ipython/profile_default/ipython_kernel_config.py.

However, re the first part of my original issue, is there any reason that the matplotlib.pyplot import call alone (i.e. without %matplotlib inline) in Jupyter Notebook should be changing matplotlib figure.dpi rc param? i.e.

EDIT: I don't get this behaviour in the IPython console:

In [1]: import matplotlib as mpl

In [2]: mpl.rcParams['figure.dpi']
Out[2]: 100.0

In [3]: import matplotlib.pyplot as plt

In [4]: mpl.rcParams['figure.dpi']
Out[4]: 100.0

@michaelaye
Copy link

michaelaye commented Oct 12, 2017

I also just noticed that this difference in DPI apparently has grown since MPL 2.x.
https://gist.github.com/ccac0541d040626b1c57e64d874fabe9

But why is even the shape of the plot different?

@darthoctopus
Copy link

darthoctopus commented Jan 10, 2018

I'm also affected by the first part of the problem after updating matplotlib and Jupyter today (happened after updating Jupyter), and would like to know how to disable this. I'm working around it for now by downgrading to ipykernel 4.6.1.

@vnoel
Copy link

vnoel commented May 28, 2018

I too find too small the matplotlib figures created in jupyter.
Unfortunately, @Carreau I do not understand your advice... What is a notebook style? How can I activate that style when I do inline? Where should I copy/paste your snippet? Thanks!

@ImportanceOfBeingErnest
Copy link
Member

@vnoel This is the issue tracker of matplotlib. You would mostly not find answers to problems here, but rather ideas to improve things in the future. This is also more related to ipykernel than matplotlib.

The basic idea of how to change the default figure settings is to change them in the ipython_kernel_config.py (see e.g. IPython config). If there is a problem with that it might be a good idea to ask a question on stackoverflow.

@talhum
Copy link

talhum commented Nov 1, 2018

Having the %matplotlib inline and mpl.rcParams['figure.dpi'] = 150 in the same cell does not work as expected: Even if the magic command (%matplotlib inline) is placed before the assignment line (mpl.rcParams['figure.dpi'] = 150), it is called last and overwrites figure.dpi.

The work-around solution is to keep the two commands in two separate cells and run the cell with %matplotlib inline before that of mpl.rcParams['figure.dpi'] = 150.

@ImportanceOfBeingErnest
Copy link
Member

Closing this in lieu of ipython/ipykernel#267. If there is any action to take here on matplotlib's side, please (request to) reopen.

@sorenwacker
Copy link

Could the magic function just no override the rc settings.

@ariel-co
Copy link

I'm not sure if this is the right place to ask, but I wonder why now the override is only apparent after the cell where pyplot is imported has run:
image

@dlukes
Copy link

dlukes commented Sep 5, 2022

Just FYI, ipython/ipykernel#267 is now resolved with matplotlib-inline 0.1.6, so there should be no more weirdness around this:

image

@christianbrodbeck
Copy link
Contributor

christianbrodbeck commented Jun 26, 2023

EDIT: this was due to an outdated version of matplotlib-inline

Now what happens for me is that the parameter changes after creating the first plot:

Screenshot 2023-06-26 at 2 05 50 PM

EDIT: matplotlib 3.7.1

@dlukes
Copy link

dlukes commented Jun 27, 2023

What about !pip show matplotlib-inline? If it's less than 0.1.6, you need to upgrade it.

@timhoffm
Copy link
Member

timhoffm commented Jun 27, 2023

It may be that this is the intended behavior. We nowadays defer backend selection until the first plot. If the activated backend manipulates rcParams, this is obviously deferred as well.

You can explicitly select the backend through https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.use. %matplotlib inline may also work.

@christianbrodbeck
Copy link
Contributor

Updating matplotlib-inline fixed it, thanks @dlukes ! I did not realize the backend is a separate package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests