แสดงเฉพาะคอลัมน์แรกในเมทริกซ์สหสัมพันธ์ใน Python

ฉันได้สร้างเมทริกซ์สหสัมพันธ์ของ dataframe ของ pandas โดยใช้ seaborn ด้วยคำสั่งต่อไปนี้:

corrMatrix = df.corr()
#sns.heatmap(corrMatrix, annot=True)
#plt.show()

ax = sns.heatmap(
    corrMatrix, 
    vmin=-1, vmax=1, center=0,
    cmap=sns.diverging_palette(20, 220, n=200),
    square=True, annot=True
)
ax.set_xticklabels(
    ax.get_xticklabels(),
    rotation=45,
    horizontalalignment='right'
);

ฉันได้รับพล็อตเมทริกซ์ต่อไปนี้:

ป้อนคำอธิบายรูปภาพที่นี่

คุณจะปกปิดเมทริกซ์สหสัมพันธ์เพื่อแสดงเฉพาะคอลัมน์แรกของเมทริกซ์ได้อย่างไร ฉันยังต้องการการแสดงตำนานทางด้านขวา

ป้อนคำอธิบายรูปภาพที่นี่


person Cody Glickman    schedule 12.04.2020    source แหล่งที่มา


คำตอบ (2)


ในกรณีของคุณ x คือ corrMatrix[['# of Prophages']]

df = pd.DataFrame({'A': np.random.rand(8), 'B': np.random.rand(8)})
corr = df.corr()

x = corr[['A']]
sns.heatmap(x)

แก้ไข:

    A            B
A   1.000000    -0.192375
B   -0.192375   1.000000

sns.heatmap(corr):

ป้อนคำอธิบายรูปภาพที่นี่

sns.แผนที่ความร้อน(x):

ป้อนคำอธิบายรูปภาพที่นี่

สิ่งนี้อาจช่วยคุณได้:

เครดิตไปที่ unutbu

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.matrix as smatrix

sns.set()

flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flights = flights.reindex(flights_long.iloc[:12].month)
sns.heatmap(flights)

ผลลัพธ์:

ป้อนคำอธิบายรูปภาพที่นี่


columns = [1953]
myflights = flights.copy()
mask = myflights.columns.isin(columns)
myflights.loc[:, mask] = 0
arr = flights.values
vmin, vmax = arr.min(), arr.max()
sns.heatmap(flights, mask=myflights, annot=True, fmt="d", vmin=vmin, vmax=vmax)

เอาต์พุต:

ป้อนคำอธิบายรูปภาพที่นี่


columns = [1953]
myflights = flights.copy()
mask = myflights.columns.isin(columns)
myflights = myflights.loc[:, mask]
arr = flights.values
vmin, vmax = arr.min(), arr.max()
sns.heatmap(myflights, annot=True, fmt="d", vmin=vmin, vmax=vmax)

เอาต์พุต:

ป้อนคำอธิบายรูปภาพที่นี่

person Pygirl    schedule 12.04.2020
comment
เอกสารดีมาก! ขอบคุณสำหรับคำตอบผ่าน! - person Cody Glickman; 12.04.2020

สิ่งนี้ใช้ได้กับฉันกับข้อมูลจำลอง:

df = pd.DataFrame(corrMatrix['# of Prophages'],
              index=corrMatrix.index)

sns.heatmap(df, annot=True, fmt="g", cmap='viridis')

plt.show()

สิ่งนี้ดัดแปลงมาจากคำตอบนี้: Seaborn Heatmap ที่มีคอลัมน์เดียว

person ananyamous    schedule 12.04.2020
comment
ขอบคุณ! ฉันกำลังมองหาคำถามนี้! ฉันเดาว่าฉันสามารถทำเครื่องหมายคำถามของฉันว่าซ้ำได้ สิ่งนี้มีประโยชน์มาก - person Cody Glickman; 12.04.2020