Friday, December 27, 2024
Google search engine
HomeLanguagesmatplotlib.pyplot.imread() in Python

matplotlib.pyplot.imread() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.imread() Function:

The imread() function in pyplot module of matplotlib library is used to read an image from a file into an array.

Syntax: matplotlib.pyplot.imread(fname, format=None)

Parameters: This method accepts the following parameters.

  • fname : This parameter is the image file to read.
  • format: This parameter is the image file format assumed for reading the data.

Returns: This method return the following.

  • imagedata : This returns the image data

Below examples illustrate the matplotlib.pyplot.imread() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt
  
with cbook.get_sample_data('loggf.png') as image_file:
    image = plt.imread(image_file)
  
fig, ax = plt.subplots()
ax.imshow(image)
ax.axis('off')
  
plt.title('matplotlib.pyplot.imread() function Example'
                                     fontweight ="bold")
plt.show()


Output:

Example #2:




# Implementation of matplotlib function
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt
  
  
with cbook.get_sample_data('loggf.png') as file:
    im = image.imread(file)
  
fig, ax = plt.subplots()
  
ax.plot(np.cos(10 * np.linspace(0, 1)), '-o', ms = 15,
                            alpha = 0.6, mfc ='green')
  
ax.grid()
fig.figimage(im, 10, 10, zorder = 3, alpha =.5)
  
plt.title('matplotlib.pyplot.imread() function Example'
                                     fontweight ="bold")
plt.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments