# How to Plot and Save a Graph in High Resolution in Matplotlib

To save a graph in high resolution in Matplotlib, we control various parameters of `savefig()` function. Similarly, we can plot graphs in high resolution by setting a high value of `dpi` parameter in `figure()` function.

## Plot Graph in High Resolution in Matplotlib

We can plot figures in high resolutions by setting high values of `dpi` in `matplotlib.pyplot.figure()` function.

Syntax of `matplotlib.pyplot.figure()`:

``````matplotlib.pyplot.figure(num=None,
figsize=None,
dpi=None,
facecolor=None,
edgecolor=None,
frameon=True,
FigureClass=<class 'matplotlib.figure.Figure'>,
**kwargs)
``````

`dpi` stands for dots per inch. It represents the number of pixels per inch in the figure. The default value for `dpi` in `matplotlib.pyplot.figure()` function is 100. We can set higher values of `dpi` to generate high-resolution plots. However, increasing `dpi` will also magnify the figure and we have to tune the appropriate value of `dpi` so that figure might not get clipped.

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 20)
m = 1.5
c = 2
y = m*x + c

plt.figure(dpi=150)
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')

plt.show()
``````

Output: ## Save Figure in High Resolution in Matplotlib

We can plot figures in high resolutions by setting high values of `dpi` in `matplotlib.pyplot.savefig()` function.

Syntax of `matplotlib.pyplot.savefig()`:

``````matplotlib.pyplot.savefig(fname,
dpi=None,
facecolor='w',
edgecolor='w',
orientation='portrait',
papertype=None,
format=None,
transparent=False,
bbox_inches=None,
frameon=None,
``````

We can control the resolution of the saved figure through `dpi` parameter in `savefig()` function. Similarly, we can also vary formats while saving the plot. Generally, `pngs` are better than `jpeg` for high-resolution plots as `png` is a lossless compression format and the other being lossy compression format.

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5)
m = 1.5
c = 2
y = m*x + c

plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.savefig("High resoltion.png",dpi=300)
``````

This saves the plot as `High resoltion.png` in the current working directory with higher resolution than the default case.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6320002,"math_prob":0.960552,"size":2258,"snap":"2020-45-2020-50","text_gpt3_token_len":545,"char_repetition_ratio":0.162378,"word_repetition_ratio":0.20454545,"special_character_ratio":0.23427813,"punctuation_ratio":0.1724138,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99957687,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-12-04T19:55:02Z\",\"WARC-Record-ID\":\"<urn:uuid:5fec9226-37c8-4082-8230-986f6835ffe5>\",\"Content-Length\":\"40615\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:346bf2bd-2b96-4e76-adcd-cfc073cc95c6>\",\"WARC-Concurrent-To\":\"<urn:uuid:3310701f-787e-43eb-a194-51e5fee613fd>\",\"WARC-IP-Address\":\"35.175.60.16\",\"WARC-Target-URI\":\"https://www.delftstack.com/howto/matplotlib/how-to-plot-and-save-a-graph-in-high-resolution/\",\"WARC-Payload-Digest\":\"sha1:ZID3B5A7MAHGS5LLAILC3TZ3JSNS56C4\",\"WARC-Block-Digest\":\"sha1:TP6VUVUOPCKNY7VUDJFXMRJOUF6XP3G3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141743438.76_warc_CC-MAIN-20201204193220-20201204223220-00163.warc.gz\"}"}