Python matplotlib/seaborn:第一行和最后一行切成热图的一半
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56942670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib/seaborn: first and last row cut in half of heatmap plot
提问by Flops
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.
当使用 seaborn(以及使用 matplotlib 的相关矩阵)绘制热图时,第一行和最后一行被切成两半。当我运行我在网上找到的这个最小代码示例时,也会发生这种情况。
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()
The labels at the y axis are on the correct spot, but the rows aren't completely there.
y 轴上的标签位于正确的位置,但行并不完全在那里。
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.
几天前,它按预期工作。从那时起,我安装了 texlive-xetex,所以我再次将其删除,但并没有解决我的问题。
Any ideas what I could be missing?
任何想法我可能会错过什么?
采纳答案by ImportanceOfBeingErnest
Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence
不幸的是,matplotlib 3.1.1破坏了 seaborn 热图;并且通常带有固定刻度的倒轴。
这在当前的开发版本中已修复;你可能因此
- revert to matplotlib 3.1.0
- use matplotlib 3.1.2 or higher
- set the heatmap limits manually (
ax.set_ylim(bottom, top) # set the ylim to bottom, top
)
- 恢复到 matplotlib 3.1.0
- 使用 matplotlib 3.1.2 或更高版本
- 手动设置热图限制 (
ax.set_ylim(bottom, top) # set the ylim to bottom, top
)
回答by Nikhil Pakki
Its a bug in the matplotlib regression between 3.1.0 and 3.1.1 You can correct this by:
它是 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)
回答by lbarbus
Fixed using the above and setting the heatmap limits manually.
修复了使用上述方法并手动设置热图限制。
First
第一的
ax = sns.heatmap(...
checked the current axes with
检查当前轴
ax.get_ylim()
(5.5, 0.5)
Fixed with
固定与
ax.set_ylim(6.0, 0)
回答by Magnus Berg Sletfjerding
I solved it by adding this line in my code, with matplotlib==3.1.1
:
我通过在我的代码中添加这一行来解决它,使用matplotlib==3.1.1
:
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions
注意。这样做的唯一原因是 x 轴没有改变,所以在未来的 mpl 版本中使用风险自负
回答by rustyDev
matplotlib 3.1.2 is out - It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install. The manual alternative worked: Download matplotlib 3.1.2 from github and install via pip
matplotlib 3.1.2 已发布 - 它可通过 conda-forge 在 Anaconda 云中使用,但我无法通过 conda install 安装它。手动替代工作:从github下载matplotlib 3.1.2并通过pip安装
% curl https://codeload.github.com/matplotlib/matplotlib/tar.gz/v3.1.2 --output matplotlib-3.1.2.tar.gz
% pip install matplotlib-3.1.2.tar.gz
回答by Lorenz
rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.
rustyDev 关于 conda-forge 是正确的,但我不需要从 github 下载进行手动 pip 安装。对我来说,在 Windows 上,它直接工作。而且情节都很好。
https://anaconda.org/conda-forge/matplotlib
https://anaconda.org/conda-forge/matplotlib
conda install -c conda-forge matplotlib
optional points, not needed for the answer:
可选点,答案不需要:
Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2
But pip says "Requirement already satisfied"
之后,我尝试了其他步骤,但不需要它们:在 conda 提示中:conda search matplotlib --info 未显示新版本信息,最新信息是 3.1.1。因此我尝试使用 pippip install matplotlib==3.1.2
但是 pip 说“要求已经满足”
Then getting the version according to medium.com/@rakshithvasudev/… python - import matplotlib - matplotlib.__version__
shows that 3.1.2 was successfully installed
然后根据medium.com/@rakshithvasudev/...获取版本python - import matplotlib - matplotlib.__version__
说明3.1.2安装成功
Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago. stackoverflow.com/questions/57225685/… which is already linked to this seaborn question.
顺便说一句,我在将 Spyder 更新到 v4.0.0 后直接出现了这个错误。错误出现在混淆矩阵的图中。这已经在几个月前提到了。stackoverflow.com/questions/57225685/... 已经与这个 seaborn 问题相关联。
回答by purushotam radadia
It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest
它发生在 matplotlib 3.1.1 版中,正如importantofbeingernest所建议的那样
Following solved my problem
以下解决了我的问题
pip install matplotlib==3.1.0
pip install matplotlib==3.1.0
回答by Sujit Bhattacharyya
conda install matplotlib=3.1.0
畅达安装 matplotlib=3.1.0
This worked for me and downgraded matplotlib from 3.1.1 to 3.1.0 and the heatmaps started to behave correctly
这对我有用并且将 matplotlib 从 3.1.1 降级到 3.1.0 并且热图开始正常运行