- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to manage image resolution of a graph in Matplotlib
An Image contains a 2-D matrix RGB data points which can be defined by the dots point per inch [ DPI ] of the image. The resolution of the image is important because a hi-resolution image will have much more clarity.
We have a method ‘plt.savefig()’ in Matplotlib which determines the size of the image in terms of its pixels. Ideally it is having an ‘dpi’ parameter.
Let’s see how we can manage the resolution of a graph in Matplotlib.
import matplotlib.pyplot as plt import numpy as np #Prepare the data for histogram np.random.seed(1961) nd = np.random.normal(13, 5, 1000) #Define the size of the plot plt.figure(figsize=(8,6)) plt.hist(nd) plt.grid() #set the dpi value to 300 plt.savefig('histogram_img.png', dpi=300) plt.show() plt.figure(figsize=(18,12)) plt.hist(nd) plt.grid() #Set the dpi value to 150 plt.savefig('histogram_100.png', dpi=150) plt.show()
Running the above code will generate the output as,
- How to plot a high resolution graph in Matplotlib?
- How to make a grouped boxplot graph in matplotlib?
- How to plot a watermark image in Matplotlib?
- How to remove whitespaces at the bottom of a Matplotlib graph?
- How to change the attributes of a networkx / matplotlib graph drawing?
- Plotting a cumulative graph of Python datetimes in Matplotlib
- Set a colormap of an image in Matplotlib
- How to get or set the resolution of an image using imageresolution() function in PHP?
- How to plot a layered image in Matplotlib in Python?
- How to give a Pandas/Matplotlib bar graph custom colors?
- How to plot a bar graph in Matplotlib from a Pandas series?
- Animate a rotating 3D graph in Matplotlib
- How to plot a line graph from histogram data in Matplotlib?
- How to shift a graph along the X-axis in matplotlib?
- How MySQL manage the behavior of a transaction?