matplotlib / seaborn:将第一行和最后一行切成热图图的一半

Flo*_*ops 59 python matplotlib seaborn

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve. This happens also when I run this minimal code example which I found online.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()
Run Code Online (Sandbox Code Playgroud)

并获得此结果(目前还不允许我嵌入图像) The labels at the y axis are on the correct spot, but the rows aren't completely there.

A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.

Any ideas what I could be missing?

Imp*_*est 56

不幸的是,matplotlib 3.1.1 打破了海洋热图;以及通常带有固定刻度的倒转轴。
在当前的开发版本中已修复此问题。你可能因此

  • 恢复到matplotlib 3.1.0
  • 使用matplotlib 3.1.2或更高版本
  • 手动设置热图限制(ax.set_ylim(...)

  • 对于 7 个级别,我必须使用 `ax.set_ylim(0 ,7)`。仅使用 ax.set_ylim(7) 将一行减半。 (3认同)

小智 31

它是3.1.0和3.1.1之间的matplotlib回归中的错误,您可以通过以下方法更正此错误:

import seaborn as sns
df_corr = someDataFrame.corr()
ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote"
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
Run Code Online (Sandbox Code Playgroud)


小智 14

使用以上方法修复并手动设置了热图限制。

第一

ax = sns.heatmap(...
Run Code Online (Sandbox Code Playgroud)

检查当前轴

ax.get_ylim()
(5.5, 0.5)
Run Code Online (Sandbox Code Playgroud)

固定于

ax.set_ylim(6.0, 0)
Run Code Online (Sandbox Code Playgroud)


小智 5

我通过在我的代码中添加这一行来解决它,使用matplotlib==3.1.1

ax.set_ylim(sorted(ax.get_xlim(), reverse=True))

注意。这样做的唯一原因是 x 轴没有改变,所以在未来的 mpl 版本中使用风险自负