Menu Close

How to specify and save a figure with exact size in pixels with Python Matplotlib?

Sometimes, we want to specify and save a figure with exact size in pixels with Python Matplotlib.

In this article, we’ll look at how to specify and save a figure with exact size in pixels with Python Matplotlib.

How to specify and save a figure with exact size in pixels with Python Matplotlib?

To specify and save a figure with exact size in pixels with Python Matplotlib, we set the figsize and dpi when calling figure.

For instance, we write

plt.figure(figsize=(800 / my_dpi, 800 / my_dpi), dpi=my_dpi)

to call figure with the figsize calculated by the width and height in pixels divided by my_dpi.

And we set dpi to my_dpi to set the dpi.

This will set the size to the width and height we want in pixels.

Conclusion

To specify and save a figure with exact size in pixels with Python Matplotlib, we set the figsize and dpi when calling figure.

Posted in Python, Python Answers