I have a pivoted pandas dataframe, containing tuples which represent RGB values:
import pandas as pd
import numpy as np
rgb = pd.DataFrame([np.random.randint(0,255,9),np.random.randint(0,255,9),np.random.randint(0,255,9)], index = ['r','g','b']).transpose()
rgbtuples = [tuple(i) for i in rgb.values]
df = pd.DataFrame([[1,2,3,1,2,3,1,2,3],[1,1,1,2,2,2,3,3,3], rgbtuples], index=['vertical', 'horizontal', 'rgb']).transpose()
df_pivot = df.pivot(index='vertical', columns = 'horizontal', values ='rgb')
resulting, for my case, in the output:
df_pivot
Out[0]:
horizontal 1 2 3
vertical
1 (128, 75, 59) (148, 77, 138) (206, 47, 212)
2 (24, 219, 53) (26, 58, 165) (127, 66, 234)
3 (39, 13, 96) (226, 251, 135) (24, 116, 245)
Where df_pivot.iloc[0,0] corresponds to (r=128,g=75,b=59).
I would like to use these values to create a heatmap, with for instance seaborne.
Any ideas?
CodePudding user response:
Seaborn's heatmap works with a colormap, while you have explicit rgb values.
You can create a numpy array from the values in df_pivot() (tuples need to be converted explicitly to arrays) and use imshow() to display the heatmap.
Here is an example (extending the data a bit, and using different horizontal and vertical size for testing purposes):
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
rgb = pd.DataFrame([np.random.randint(0, 255, 20), np.random.randint(0, 255, 20), np.random.randint(0, 255, 20)],
index=['r', 'g', 'b']).transpose()
rgbtuples = [tuple(i) for i in rgb.values]
df = pd.DataFrame([np.tile(np.arange(1, 5), 5), np.repeat(np.arange(1, 6), 4), rgbtuples],
index=['vertical', 'horizontal', 'rgb']).transpose()
df_pivot = df.pivot(index='vertical', columns='horizontal', values='rgb')
fig, ax = plt.subplots()
df_pivot_asarray = np.array([[list(tup) for tup in row] for row in df_pivot.to_numpy()])
xlen = len(df_pivot.columns)
ylen = len(df_pivot.index)
ax.imshow(df_pivot_asarray, extent=[- 0.5, xlen - 0.5, -0.5, ylen - 0.5], origin='lower')
ax.set_xticks(range(xlen))
ax.set_xticklabels(df_pivot.columns)
ax.set_yticks(range(ylen))
ax.set_yticklabels(df_pivot.index)
ax.invert_yaxis() # seaborn shows the first row at the top
plt.show()
CodePudding user response:
this code will get you a grid of numbers:
def colorValue(value):
retVal=((value[0]&0x0ff)<<16)|((value[1]&0x0ff)<<8)|(value[2]&0x0ff)
return retVal
colors=[]
df=pd.DataFrame(columns=['cell1','cell2','cell3'])
for index,(cell1,cell2,cell3) in df_pivot.items():
print(index,cell1,cell2,cell3)
df=df.append({'cell1':colorValue(cell1),'cell2':colorValue(cell2), 'cell3':colorValue(cell3)},ignore_index=True)

