Skip to content
Related Articles

Related Articles

Matplotlib.figure.Figure.dpi() in Python
  • Last Updated : 30 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.

matplotlib.figure.Figure.dpi method

The dpi method of figure module of matplotlib library is the resolution in dots per inch.

Syntax: fig.dpi

Parameters: This method does not accept any parameters.

Returns: This method returns resolution in dots per inch.



Below examples illustrate the matplotlib.figure.Figure.dpi function in matplotlib.figure:

Example 1:




# Implementation of matplotlib function 
import matplotlib.pyplot as plt
import numpy as np
   
  
fig = plt.figure()
  
nx = int(fig.get_figwidth() * fig.dpi)
ny = int(fig.get_figheight() * fig.dpi)
  
data = np.random.random((ny, nx))
plt.plot(data)
   
fig.suptitle('matplotlib.figure.Figure.dpi \
function Example', fontweight ="bold"
  
plt.show()

Output:

Example 2:




# Implementation of matplotlib function  
import matplotlib.pyplot as plt
from matplotlib import collections, colors, transforms
import numpy as np
   
nverts = 50
npts = 100
   
r = np.arange(nverts)
theta = np.linspace(0, 2 * np.pi, nverts)
  
xx = r * np.sin(theta)
yy = r * np.cos(theta)
  
spiral = np.column_stack([xx, yy])
   
rs = np.random.RandomState(19680801)
   
xyo = rs.randn(npts, 2)
   
colors = [colors.to_rgba(c)
          for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
   
fig, ax1 = plt.subplots()
   
col = collections.RegularPolyCollection(
    7, sizes = np.abs(xx) * 10.0, offsets = xyo, 
    transOffset = ax1.transData)
  
trans = transforms.Affine2D().scale(fig.dpi / 72.0)
  
col.set_transform(trans) 
ax1.add_collection(col, autolim = True)
col.set_color(colors)
  
fig.suptitle('matplotlib.figure.Figure.dpi() function\
 Example', fontweight ="bold"
  
plt.show()

Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :