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

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS