【发布时间】: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