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

Low quality matplotlib inline plots on hidpi display #9148

Closed
8 of 10 tasks
chesnokov-yuriy opened this issue Apr 12, 2019 · 22 comments · Fixed by #21812
Closed
8 of 10 tasks

Low quality matplotlib inline plots on hidpi display #9148

chesnokov-yuriy opened this issue Apr 12, 2019 · 22 comments · Fixed by #21812

Comments

@chesnokov-yuriy
Copy link

chesnokov-yuriy commented Apr 12, 2019

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

What steps reproduce the problem?

  1. set a custom high DPI scalling (default 1.5 or any larger value) in spyder Preferences -> General
  2. plot a line with matplotlib
import numpy as np
from matplotlib import pyplot as pp
pp.figure()
pp.plot(np.arange(100), 'k')
pp.grid()
  1. change inline backend figure format to retina
%config InlineBackend.figure_format = 'retina'
  1. repeat step 2.

What is the expected output? What do you see instead?

Expected the output to be smooth high quality DPI inline plot after change in inline figure format but instead blurry jagged low quality version is displayed as before

spyder-matplotlib-inline-plot

Jupyter notebook run shows correct smooth plot after inline figure format change

jupyter-ipython-inline-plot

Versions

  • Spyder version: 3.3.4
  • Python version: 3.6.7 64-bit
  • Qt version: 5.12.2
  • PyQt version: 5.12.1
  • Operating System name/version: Linux Mint 19.1 Cinnamon 4.0.10

Dependencies

IPython >=4.0     :  7.4.0 (OK)
cython >=0.21     :  0.29.6 (OK)
jedi >=0.9.0      :  0.13.3 (OK)
matplotlib >=2.0.0:  3.0.3 (OK)
nbconvert >=4.0   :  5.4.1 (OK)
numpy >=1.7       :  1.16.2 (OK)
pandas >=0.13.1   :  0.24.2 (OK)
psutil >=0.3      :  5.6.1 (OK)
pycodestyle >=2.3 :  2.5.0 (OK)
pyflakes >=0.6.0  :  2.1.1 (OK)
pygments >=2.0    :  2.3.1 (OK)
pylint >=0.25     :  2.3.1 (OK)
qtconsole >=4.2.0 :  4.4.3 (OK)
rope >=0.9.4      :  0.14.0 (OK)
sphinx >=0.6.6    :  2.0.1 (OK)
sympy >=0.7.3     :  1.4 (OK)
@ccordoba12
Copy link
Member

@dalthviz, please take a look at this one. We need to add a new option in the Inline backend section of our preferences called

[ ] Display images in high resolution

And pass that option to the kernel.

@chesnokov-yuriy
Copy link
Author

using Kubuntu 18.10 installation there is no such a problem even with default png backend inline format
Python 3.6.7 64-bit | Qt 5.12.2 | PyQt5 5.12.1 | Linux 4.18.0-17-generic

@ccordoba12
Copy link
Member

Then I'd say it's a Qt issue and not an Spyder one.

@goanpeca
Copy link
Member

Display images in high resolution

If this is still a correction needed, why not make it a default (instead of adding the option?)

@ccordoba12
Copy link
Member

Because in non-HDPI screens plots will look huge.

@goanpeca
Copy link
Member

Because in non-HDPI screens plots will look huge.

Got it thanks!

Is there an overall High DPI setting for spyder? Could we link to that one?

@ccordoba12
Copy link
Member

ccordoba12 commented Apr 15, 2019

There's what Rafa added in

Preferences > General > Screen resolution

But linking both things (a change in that option with a change in the inline backend) is not work for Spyder 3.

Should we leave this for Spyder 4? It's not that critical.

@goanpeca
Copy link
Member

Should we leave this for Spyder 4? It's not that critical.

Yes, I believe is the right place to fix it.

@ccordoba12 ccordoba12 modified the milestones: v3.3.5, v4.0beta3 Apr 15, 2019
@chesnokov-yuriy
Copy link
Author

Then I'd say it's a Qt issue and not an Spyder one.

There are similar issues with Gwenview PNG images opened in Kubuntu 18.10 HIDPI monitor with desktop scaled to fractional scale as in Spyder running on Mint 19.1. PNG images in Gwenview once opened looked exactly blurry and jagged and that issue is known and reported.

@ccordoba12
Copy link
Member

@goanpeca, do you need to activate our option in

Preferences > General > Screen resolution

in your Retina Display Mac for things to look good there?

That's to know if Qt adjusts automatically to HiDPI in some cases (like macOS).

If you don't need to activate that option, then we really need to add another option to the console preferences (as proposed at the beginning) to tell the kernel that Spyder is running in a HiDPI screen.

@ccordoba12
Copy link
Member

Some useful links:

@ccordoba12 ccordoba12 added this to the v5.3.2 milestone Apr 9, 2022
@ccordoba12 ccordoba12 modified the milestones: v5.3.2, v5.3.3 May 4, 2022
@ccordoba12 ccordoba12 modified the milestones: v5.3.3, v5.3.4 Jun 20, 2022
@ccordoba12 ccordoba12 modified the milestones: v5.4.3, v5.4.2 Nov 18, 2022
@ccordoba12 ccordoba12 modified the milestones: v5.4.2, v6.0alpha1 Jan 1, 2023
@ccordoba12 ccordoba12 modified the milestones: v6.0alpha1, v6.0alpha2 Jun 8, 2023
@ccordoba12 ccordoba12 modified the milestones: v6.0alpha2, v6.0alpha3 Jul 29, 2023
@ccordoba12 ccordoba12 modified the milestones: v6.0alpha3, v6.0beta1 Nov 17, 2023
@jitseniesen
Copy link
Member

This discussion runs over several years and it's not clear to me what needs to be done now. This is my understanding of the current situation.

The images in the plots pane can be fixed by increasing the resolution (DPI) in the preferences, so the only thing that we maybe need to do is to change the default resolution, perhaps by taking the high-DPI scaling factor into account.

The images in the IPython console can not be fixed by increasing the DPI in the preferences, because this will simply enlarge the image and the resolution remains the same. One possible fix here is to support the "Retina" format for plots like Jupyter, along the lines of PR #12128. However, that is a bit of a kludge.

@ccordoba12
Copy link
Member

One possible fix here is to support the "Retina" format for plots like Jupyter, along the lines of PR #12128. However, that is a bit of a kludge.

In fact, I think that would be the simplest thing to do, i.e. allow users to control the retina display option with a Spyder one. To do that, I propose the following:

  • Add an option to the Plots options menu called Enable high resolution plots, checked by default.
  • That option should set/unset the retina display option in the Inline backend.
  • If users uncheck the Mute inline plotting option, Enable high resolution plots should be unchecked and disabled so plots appear in the console at an appropriate size.
  • When Mute inline plotting is unchecked, a message should be displayed in the console to warn users that plots will be shown in low resolution.

@jitseniesen
Copy link
Member

I have now a slightly better idea what the "Retina" format for plots does. It changes two things: it doubles the matploblib resolution (in dpi) from what the user sets, and it shrinks plots in the IPython console by a factor of two.

In more detail, the standard settings are for plots to be 6 inches wide and 4 inches high with a resolution of 72 dpi. This produces images of 6 * 72 = 432 pixels wide and 4 * 72 = 288 pixels high (actually, the images are slightly smaller if tight layout is enabled, which it is by default). If the plot is displayed in the IPython console (which if the option "Mute inline plotting" is unchecked), then it is displayed as an image of 432x288 pixels. By default, the plot is displayed in the Plot pane with "Fit plots to window" checked. If the area where the plot is displayed is bigger than 432x288 (which corresponds to the Spyder window being bigger than about 1000x800 pixels), then the image is magnified and since it is PNG that will give a low quality plot.

If the user turns on retina (which can currently be done with %config InlineBackend.figure_format = 'retina') then the plots are generated with 2 * 72 = 144 dpi, so the image is 864x576. This probably looks better in the Plots pane (I say "probably" because the zoom factor is non-integer so the plot never looks very good unless "Fit plots to window" is unchecked). However, in the IPython console the image is shrunk by a factor of two, so it will be displayed as 432x288 pixels, just as before. This does not give a better image on my screen, even if I set a custom high DPI scaling of 1.5 in Spyder. Of course, this may depend on OS or physical monitor.

So at least for me, retina does not improve the appearance in the IPython console and while is does improve the appearance in the Plots pane, the same effect can be achieved more transparently by increasing the DPI value in the Spyder preferences.

@ccordoba12
Copy link
Member

So at least for me, retina does not improve the appearance in the IPython console and while is does improve the appearance in the Plots pane, the same effect can be achieved more transparently by increasing the DPI value in the Spyder preferences.

Ok, thanks for the analysis @jitseniesen. Could post a screenshot of how plots look with an increased DPI (the one you consider it'd really improve things) in the console and Plots pane?

@jitseniesen
Copy link
Member

Here is a plot in the Plots pane in the default resolution of 72 dpi. I reconfigured the layout on Spyder somewhat to make the Plots pane bigger so that the difference is more apparent on my monitor (1920 x 1080 pixels). This corresponds on what would happen in the default layout if your monitor is 2560 x 1440 or thereabouts. The picture is shown with a zoom factor of 213% which makes it not sharp.

plot-default

This is what is looks like in the IPython console. The console does not zoom so the picture is always sharp, but small.

console-default

If we double the resolution of the plots to 144 dpi, it is displayed with a zoom factor of 107% and looks quite sharp.

plot-double

The IPython console does not zoom so the image is twice as big:

console-double

Increasing the DPI is not free, it makes the pictures bigger and thus that costs more memory. The PNG of the plot with 144 dpi is 29 kB, so if you have 100 plots that makes 2.9 MB, which by itself seems quite okay. There is probably more overhead, which might be substantial and thus cause problems, but I don't know.

If we don't care about memory and we want most users to get sharp images, I propose to increase the default DPI in Spyder so that plots on a fairly typical but high-end monitor are displayed with a zoom factor of around 100%. If we use 4K monitors as our reference, their resolution is 3840 x 2160. I estimate that the area in the Plots display for showing the actual plot is about 1700 x 900. With a default plot height of 4 inches, that suggests a resolution of 225 dpi. For comparison, the current default in Spyder is 72 dpi and in Matplotlib it is 100 dpi.

An alternative is to do what the IPython console does and not zoom in, this corresponds to having the option "Fit plots to window" unchecked by default (it is now checked by default). This should help with users complaining that the plot is not sharp, but instead we will get users complaining that the plot is too small.

Final note, I don't have a true hi-dpi monitor so I can't test that.

@ccordoba12
Copy link
Member

If we don't care about memory and we want most users to get sharp images, I propose to increase the default DPI in Spyder so that plots on a fairly typical but high-end monitor are displayed with a zoom factor of around 100%.

Just a quick note to say that agree with this decision.

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

Successfully merging a pull request may close this issue.

6 participants