Matplotlib is a Python package that is used for data visualization, and it offers several functionalities to create charts, graphs, and plots. One such functionality is the Figure object which is the top-level container for all the plot elements.
The get_dpi() method of the Figure object returns the figure’s inherent resolution in dots per inch. This method is useful when we want to find the DPI of a plot to customize the plot’s figure size.
Example:
Let’s say we create a plot of sine wave using Matplotlib, and we want to find out the exact figure resolution for our plot. We can use the get_dpi() method to determine the DPI of the plot.
import matplotlib.pyplot as plt import numpy as np
# Generate data x = np.arange(0, 10, 0.1) y = np.sin(x)
# Create the figure object fig = plt.figure()
# Add a subplot to the figure ax = fig.add_subplot(111)
# Plot the sine wave ax.plot(x, y)
# Get the DPI of the figure dpi = fig.get_dpi()
# Print the DPI print("The DPI of the figure is:", dpi)
# Display the plot plt.show()
Output:
The DPI of the figure is: 100
In the example above, we first generate the data for the sine wave and then create the figure object using plt.figure(). Next, we create the subplot using the add_subplot() method and then plot the sine wave using the plot() method.
Finally, we use fig.get_dpi() to get the DPI of the figure, and we print the DPI to the console. In this case, the DPI is 100.
Package Library: Matplotlib.
Python Figure.get_dpi - 58 examples found. These are the top rated real world Python examples of matplotlib.figure.Figure.get_dpi extracted from open source projects. You can rate examples to help us improve the quality of examples.