IPython and Inline Matplotlib Screen Sizes

In my current iPython, matplotlib graphs are displayed in a line. I need a way to display images with specific pixel sizes, since I usually only work with pixels, and I don't print anything.

My PPI screen is 208, so I ran these two snippets of code:

plt.figure(figsize=(2000/float(208), 1000/float(208)), dpi=208) # other code here... plt.savefig('my_fig.png', dpi=208) 

What I'm confused about is this: when I look at my_fig.png , it is actually 2000 pixels by 1000 pixels, this is what I want. Also, in accordance with https://stackoverflow.com/a/3129478/2/11/11/16/163946/ DPI settings for displaying on a display device and displaying a file have different default values. Therefore, I have to add the plt.savefig function in dpi=208 .

However, the image displayed on the iPython laptop, which is in the browser, is much smaller. Using my browser line, it is approximately 600 by 300 pixels.

Using the same DPI for both functions, why is it that the embedded image in the iPython laptop is so small and my saved image matches the correct resolution that I want?

Please note that I am running iPython in Virtualbox (it is also headless), I am not sure if this can cause any differences.

After some trial and error, to get closer to the correct size for inline rendering, the number of dots per inch should be 58. But even then, the images generated in the line do not match the exact number of pixels. It is always + - from 10 to 20 pixels.

+5
source share
1 answer

This may be due to the fact that %matplotlib inline by default passes bbox_inches='tight' to plt.figure(...) . Try the following:

After calling %matplotlib inline do %config InlineBackend.print_figure_kwargs = {'bbox_inches':None} then create your storyline. Here is what he did for me:

Before Before

After After

0
source

Source: https://habr.com/ru/post/1244651/


All Articles