Using imshow with a df

I have a 100x100 B&W image stored in a df (100 rows and 100 columns). I then use imshow to display it.

I would like to switch this to a single color image. To do this, I believe I need to change every cell which now contains 1 value β€˜x’, to a list that contains this initial value, plus 2 more zeros β€˜[x,0,0]’. What is the best way to make this change in each cell in my df?

Hi @majudd , I would be interested to know, why you have thes values stored in a DataFrame?

If I understand you correcly, you want to switch from grayscale to an other colorscheme, let’s say β€œredscale” as you indicate by

If so, you could simply switch the colorscales:

import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig= make_subplots(rows=1, cols=4)

a = np.random.randint(0, 255, 10000).reshape(100, 100)
df = pd.DataFrame(a, columns=[*range(100)])

fig.add_trace(go.Heatmap(z=df.values, colorscale='Gray'), row=1, col=1)
fig.add_trace(go.Heatmap(z=df.values, colorscale='Reds'), row=1, col=2)
fig.add_trace(go.Heatmap(z=df.values, colorscale='Greens'), row=1, col=3)
fig.add_trace(go.Heatmap(z=df.values, colorscale='Blues'), row=1, col=4)


As for pandas, you could use applymap:
df = df.applymap(lambda x: [x, 0, 0])

Thanks! I have a B&W image stored in a dfold as code values from 0-255, in rows and columns. Then I have a second (df2) with the same number of rows and columns, and the value in each cell in that df will determine the color of each pixel in a new colored version of the original B&W image. I was just trying to make a red image first just to get the hang of turning the cells which contain integers, to now contain 3 element lists. I succeeded. The trick was to make a new df an object, which then permitted me to switch each cell from an integer to a list. I used dfnew = dfold.astype(object). Then I operated on dfnew to switch each cell from β€œx” to β€œ[x,0,0]”.

For the rest of this, it seems that I need to go through dfold, row by row, and column by column, deciding what color each cell will be and then making the switch from integer to list. For example, I am setting the color blue if the value in the cell for df2 is between 0-2, green if it is between 2-4, and red if it is between 4-6. Is there a faster way of doing this than iterating over rows and columns. Perhaps by making a mask?

Just to help understand, the values in df2 are a sort of error magnitude. We are using color to show where we have the most errors in our B&W images.