如何从 RGB 值创建热图?
How to create a heatmap from RGB values?
我有一个旋转的 pandas 数据框,包含表示 RGB 值的元组:
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')
就我而言,结果是输出:
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)
其中df_pivot.iloc[0, 0]
对应(r=128, g=75, b=59)
.
我想使用这些值来创建热图,例如 seaborne。
有什么想法吗?
此代码将为您提供数字网格:
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)
Seaborn 的热图使用颜色图,而您有明确的 rgb 值。
您可以从 df_pivot()
中的值创建一个 numpy 数组(元组需要显式转换为数组)并使用 imshow()
显示热图。
这是一个示例(稍微扩展了数据,并使用不同的水平和垂直尺寸进行测试):
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()
我有一个旋转的 pandas 数据框,包含表示 RGB 值的元组:
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')
就我而言,结果是输出:
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)
其中df_pivot.iloc[0, 0]
对应(r=128, g=75, b=59)
.
我想使用这些值来创建热图,例如 seaborne。
有什么想法吗?
此代码将为您提供数字网格:
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)
Seaborn 的热图使用颜色图,而您有明确的 rgb 值。
您可以从 df_pivot()
中的值创建一个 numpy 数组(元组需要显式转换为数组)并使用 imshow()
显示热图。
这是一个示例(稍微扩展了数据,并使用不同的水平和垂直尺寸进行测试):
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()