Plotting an image with imshow from a pandas dataframe

I am trying to plot an image loaded in a pandas DataFrame. However, using px.imshow I get an empty plot only when using the values from pd.DataFrame.values even though using the same values as a numpy array works fine.

Here’s a code to reproduce the scenario:

color_matrix = [
    [[255, 0, 0], [0, 255, 0],],
    [[255, 0, 255], [255, 255, 0],],
img_rgb = np.array(color_matrix, dtype=np.uint8)
fig = px.imshow(img_rgb)

This code works successfully, but the following does not

img_rgb_df = pd.DataFrame(color_matrix)
fig = px.imshow(img_rgb_df.values)

What is the difference between the two values provided to px.imshow ?? and is that the reason the plot is empty ?

Many thanks

Hey @TheAtropos1994, the reason is that the elements of img_rgb_df are lists:

>>> img_rgb_df.values
array([[list([255, 0, 0]), list([0, 255, 0])],
          [list([255, 0, 255]), list([255, 255, 0])]], dtype=object)

That said, I don’t know how to convince pandas how to create the right data format…

Thank you Emanuelle, the following code, converting the inner lists to numpy arrays with type uint8:

        [[np.array(i, dtype=np.uint8) for i in j] for j in img_rgb_df.values],
1 Like