【发布时间】:2022-01-18 15:11:07
【问题描述】:
我有一个旋转的 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。
有什么想法吗?
【问题讨论】:
-
感谢您的回答。如果我调用 sns.heatmap(df_pivot),x 和 y 参数是元组,这会给出错误:设置带有序列的数组元素
标签: python pandas matplotlib seaborn