3
我一直與seaborn
及其heatmap
功能。我想從大熊貓數據框中df
帶有加註解的價值觀來建立矩陣:熊貓seaborn - 熱圖無顏色
C,L,N
a,x,10
a,y,2
a,z,4
b,x,1
b,y,22
b,z,11
c,x,3
c,y,1
c,z,0
到目前爲止,它工作得很好用:
# Read DataBase
df = pd.read_csv('myfile.csv')
# Save Users/City/Language Matrix
pdf = df.pivot(index='C',columns='L',values='N').fillna(0)
# Set Font Parameters
rc = {'font.size': 8, 'xtick.labelsize': 11, 'ytick.labelsize': 11}
# Set Figure
fig = plt.figure(figsize=(20, 9))
# Assign to Seaborn
sns.set(rc=rc)
with sns.axes_style('white'):
sns.heatmap(pdf,
cbar=False,
square=False,
annot=True,
cmap='Blues',
fmt='g',
linewidths=0.5)
將返回:
在結束我有興趣只保留值並將結構保存爲一個簡單的表格,丟棄顏色。我試圖設置cmap=None
,但它不起作用,並且沒有cmap
,seaborn會將默認cmap
分配給熱圖。
from matplotlib.colors import ListedColormap
with sns.axes_style('white'):
sns.heatmap(pdf,
cbar=False,
square=False,
annot=True,
fmt='g',
cmap=ListedColormap(['white']),
linewidths=0.5)
這將產生:
呃..我不知道如果我沒有正確理解你......是你想要的只是一個表? –