Last updated: Apr 11, 2024
Reading time·4 min
The matplotlib "TypeError: Image data cannot be converted to float" occurs
when you pass an incorrect argument to the plt.imshow()
method.
To solve the error, load the image and pass it as an argument to
plt.imshow()
.
Here is an example of how the error occurs.
import matplotlib.pyplot as plt # ⛔️ TypeError: Image data of dtype <U9 cannot be converted to float plt.imshow('house.webp') plt.show()
We called the matplotlib.pyplot.imshow() method with an incorrect argument which caused the error.
The most common cause of the error is calling the method with a path to the image rather than the image itself.
To solve the error, load the image and pass it as an argument to
pyplot.imshow()
instead.
import matplotlib.pyplot as plt img = plt.imread('house.webp') plt.imshow(img) plt.show()
The code sample assumes that you have the following house.webp
image in the
same directory as your main.py
script.
You can right-click on the image and select "Save image as" to download it.
Here is the result of running the python main.py
command.
We used the matplotlib.pyplot.imread() method to read the image from a file into an array.
The only argument we passed to the method is the filename.
The argument the pyplot.imread()
method takes can be a filename or a file-like
object opened in read-binary mode.
import matplotlib.pyplot as plt img = plt.imread('house.webp') plt.imshow(img) plt.show()
If you need to read an image from a URL, use the Pillow library instead.
import urllib.request import matplotlib.pyplot as plt import numpy as np from PIL import Image url = 'https://www.google.com/favicon.ico' img = np.array(Image.open(urllib.request.urlopen(url))) plt.imshow(img) plt.show()
Make sure you have numpy and pillow installed by running the following command in your terminal.
pip install numpy Pillow matplotlib # or with pip3 pip3 install numpy Pillow matplotlib
The code sample:
urllib.request.urlopen()
method to load the image from the remote
URL.PIL.Image()
class to open and identify the image file.numpy.array()
method to convert the PIL image
to an array.We can then pass the image to the pyplot.imshow()
method to display it.
The pyplot.imshow method can be called with an array-like object or a PIL image.
The supported array shapes are:
The first two dimensions (M, N) define the rows and columns of the image.
Make sure you aren't trying to plot an image path.
Instead, load the image and plot the image itself.
You can also use the Pillow module to load the image from a local file.
import matplotlib.pyplot as plt import numpy as np from PIL import Image img = np.array(Image.open('house.webp')) plt.imshow(img) plt.show()
The Image.open() method opens and identifies the given image file.
The only parameter we passed to the method is a filename string.
The method can also be called with a pathlib.Path
object or a file object.
You can also use the opencv-python
module to load the image before showing it.
First, make sure you have the module installed.
Open your terminal and run the following command.
pip install opencv-python # or with pip3 pip3 install opencv-python
Now import and use the module as follows.
import cv2 from matplotlib import pyplot as plt img = cv2.imread('house.webp') plt.imshow(img) plt.show()
The cv2.imread
method takes the path to the image as a parameter.
The method loads the image from the specified file and returns it.
If you run into issues when specifying the path of the file:
cv2.imread()
method.If you are on Windows, use the r
character to mark the path as a raw string.
file_name = r'C:\Users\YourUser\Desktop\your-image.png'
When we prefix the string with r
, we treat backslashes as literal characters
instead of escape characters.
You can also use a second backslash to escape the first.
file_name = 'C:\\Users\\YourUser\\Desktop\\your-image.png'
It is also valid to use forward slashes instead of backslashes.
file_name = 'C:/Users/YourUser/Desktop/your-image.png'
However, make sure you aren't using a single backslash in your path as single backslash characters are used as an escape sequence.
You can learn more about the related topics by checking out the following tutorials: