Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase resolution of figure while preserving dimensions in Python matplotlib

I have created a figure using the following method:

import matplotlib.pyplot as plt
%matplotlib inline
fig1 = plt.figure(figsize=(2, 2), dpi=100)

I want to increase the resolution of the figure, so I increased the dpi of fig1 from 100 to 200. However, this changed the pixel dimensions from (200, 200) to (400, 400), which is something I do not want. If I let dpi=200 and figsize=(1, 1), the pixel dimensions remains (200, 200), but the image looks funny. Please see the output from my Jupyter notebook here.

Is there a way in Matplotlib to increase fig1's resolution without altering its pixel dimension when rendered in Jupyter?

(Note: the code I used for generating the figures linked to above came from this question.)

like image 764
Vivek Avatar asked Jun 12 '18 21:06

Vivek


People also ask

How do I increase the resolution of an image in matplotlib?

To improve matplotlib image quality we can use greater dot per inch i.e dpi value (greater than 600) and pdf or . eps format can be recommended.

How do you increase a Figsize in Python?

If you've already got the figure created, say it's 'figure 1' (that's the default one when you're using pyplot), you can use figure(num=1, figsize=(8, 6), ...) to change it's size etc.

Why are my python plots blurry?

Matplotlib can be a powerful tool for Python-based plotting, but if you've ever generated your plots inline within a Jupyter notebook you probably noticed that the default resolution of the rendered plot images is pretty low, which can leave your plots looking blurry/grainy and hard to read.


1 Answers

Specifically in the case of 2x (e.g. for retina screens), yes, use

%config InlineBackend.figure_format = 'retina'

like image 67
wordy Avatar answered Oct 20 '22 09:10

wordy