【问题标题】:Why doesn't the heatmap of correlation matrix plot show the last row and column为什么相关矩阵图的热图不显示最后一行和最后一列
【发布时间】:2019-07-19 20:08:00
【问题描述】:

我试图绘制一个相关矩阵。但由于某种原因,它没有绘制最后一行和最后一列。我真的不确定这里发生了什么。有趣的是,matplotlib 和 seaborn 都会出现这个问题。代码、热图和相关矩阵(“相关性”)如下所示。有人可以帮我找出问题吗?

df = df_[cols]
correlations = df.corr(method='spearman')
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.matshow(correlations, vmin=-1, vmax=1)
ticks = np.arange(0, len(cols), 1)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels(cols)
ax.set_yticklabels(cols)
plt.setp(ax.get_xticklabels(), rotation=90, horizontalalignment='right')
fig.tight_layout()
#sb.heatmap(correlations, xticklabels=cols, yticklabels=cols, vmin=-1, vmax=1)
plt.savefig(folder_ranges+'rankcorr.png')

【问题讨论】:

  • 第二张图片只有 7 列。可能有一些NaN。您的第二张图片中的L2_hidden 在哪里?
  • 哦。哇,真的很奇怪 df.corr() 似乎完全删除了这个列,尽管列中没有 NaN。我不确定这里发生了什么。
  • 知道了! L2_hidden 列虽然给出了 type=float,但它似乎不是一个数值。我使用“df['L2_hidden'] = df['L2_hidden'].convert_objects(convert_numeric=True)”将该列转换为数字,现在它可以工作了。感谢@Sheldore 的帮助

标签: python pandas matplotlib seaborn


【解决方案1】:

L2_hidden 列虽然给出了 type=float,但它似乎不是一个数值,df.corr() 从相关矩阵中删除了它。我使用“df['L2_hidden'] = df['L2_hidden'].convert_objects(convert_numeric=True)”将该列转换为数字,现在它可以工作了。

【讨论】:

  • 这样? @Blastfurnace
猜你喜欢
  • 2019-04-13
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
相关资源
最近更新 更多