【发布时间】:2018-06-23 03:21:16
【问题描述】:
如果您能告诉我如何为具有大约 150 个特征的大型数据集绘制高分辨率热图,我将不胜感激。
我的代码如下:
XX = pd.read_csv('Financial Distress.csv')
y = np.array(XX['Financial Distress'].values.tolist())
y = np.array([0 if i > -0.50 else 1 for i in y])
XX = XX.iloc[:, 3:87]
df=XX
df["target_var"]=y.tolist()
target_var=["target_var"]
fig, ax = plt.subplots(figsize=(8, 6))
correlation = df.select_dtypes(include=['float64',
'int64']).iloc[:, 1:].corr()
sns.heatmap(correlation, ax=ax, vmax=1, square=True)
plt.xticks(rotation=90)
plt.yticks(rotation=360)
plt.title('Correlation matrix')
plt.tight_layout()
plt.show()
k = df.shape[1] # number of variables for heatmap
fig, ax = plt.subplots(figsize=(9, 9))
corrmat = df.corr()
# Generate a mask for the upper triangle
mask = np.zeros_like(corrmat, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
cols = corrmat.nlargest(k, target_var)[target_var].index
cm = np.corrcoef(df[cols].values.T)
sns.set(font_scale=1.0)
hm = sns.heatmap(cm, mask=mask, cbar=True, annot=True,
square=True, fmt='.2f', annot_kws={'size': 7},
yticklabels=cols.values,
xticklabels=cols.
values)
plt.xticks(rotation=90)
plt.yticks(rotation=360)
plt.title('Annotated heatmap matrix')
plt.tight_layout()
plt.show()
它工作正常,但为具有 40 多个特征的数据集绘制的热图太小。
提前致谢,
【问题讨论】:
-
@Mr.T 非常感谢您的时间和考虑。我试过 'figsize=(200, 200),dpi=150' 但我不知道为什么它没有改善很多。我的数据集的一部分在这里:kaggle.com/shebrahimi/financial-distress
-
@Mr.T 此外,如果有用的话,我不知道如何使用 plotly。 plot.ly
-
您尝试保存图形,还是仅使用 plt.show()?当你保存为 pdf 时会发生什么?
-
感谢您的时间和考虑。我已经尝试过 fig.savefig('heat.png') 和 fig.savefig('heat.pdf') 但它没有任何区别。 @MarkTeese
标签: python-3.x data-visualization heatmap seaborn