绘制热图

问题描述 投票:0回答:1

enter image description here我在使用 Seaborn 渲染热图时遇到问题,其中图表显示不完整。任何人都可以提供有关可能导致这种不完整渲染的原因的见解或建议可能的解决方案来解决它吗?

下面是我正在使用的代码的简化版本:

    selected_features = ['Survived', 'Pclass', 'Age', 'SibSp', 'Parch','Fare']
    subset_df = df[selected_features]
    
    #Computing the correlation matrix for the selected features
    correlation_matrix = subset_df.corr()
      sns.set(rc={'figure.figsize':(10,8)})
sns.heatmap(correlation_matrix ,annot = True, cmap = 'coolwarm', square=True)
python matplotlib plot seaborn
1个回答
0
投票

enter image description here

给出的脚本看起来不完整,让我与示例数据帧分享正确的代码

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {
    'Survived': [1, 0, 1, 0, 0],
    'Pclass': [1, 2, 3, 1, 2],
    'Age': [22, 38, 26, 35, 27],
    'SibSp': [1, 0, 3, 1, 0],
    'Parch': [0, 0, 1, 1, 0],
    'Fare': [7.25, 71.28, 7.92, 53.1, 8.05]
}

df = pd.DataFrame(data)

selected_features = ['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare']
subset_df = df[selected_features]
correlation_matrix = subset_df.corr()
sns.set(rc={'figure.figsize':(10,8)})
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", square=True)
plt.title('Correlation Heatmap of Selected Features')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.