Python seaborn 热图 y 轴逆序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34232073/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:39:56  来源:igfitidea点击:

seaborn heatmap y-axis reverse order

pythonmatplotlibheatmapseaborn

提问by john kals

Have a look at thisheatmap found in the seaborn heatmap documentation.

看看在 seaborn heatmap 文档中找到的这个热图。

Right now the y-axis starts with 9 at the bottom, and ends with 0 on top. Is there a way to turn this around, i.e. start with 0 at bottom and end with 9 at the top?

现在 y 轴从底部的 9 开始,到顶部的 0 结束。有没有办法扭转这种局面,即从底部的 0 开始,顶部的 9 结束?

回答by user3412205

Looks like ax.invert_yaxis()solves it.

好像ax.invert_yaxis()解决了

Following the example from which you got the figure:

按照您从中获得图形的示例:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
ax.invert_yaxis()

Gives: enter image description here

给出: 在此处输入图片说明

回答by ryanjdillon

If you are using a 'hex' jointplot()for a heatmap like I was, then you can do this:

如果您jointplot()像我一样使用“十六进制”作为热图,那么您可以这样做:

import matplotlib.pyplot as plt
import numpy
import seaborn

x = numpy.arange(10)
y = x**2

g = seaborn.jointplot(x, y, kind='hex')
g.fig.axes[0].invert_yaxis()

plt.show()

enter image description here

在此处输入图片说明

回答by terence hill

I found a simpler method to set the axes order, using the options ylimand xlim. In the following examples I plot H, a 2d matrix (NX x NY), changing the axes order:

我找到了一种更简单的方法来设置轴顺序,使用选项ylimxlim。在以下示例中,我绘制了 H,一个二维矩阵 (NX x NY),更改了轴顺序:

import matplotlib.pyplot as plt
import seaborn as sns

NX=10
NY=20
H = np.random.rand(NY, NX)
sns.heatmap(H, xticklabels=True, yticklabels=True, annot = True)
plt.ylim(0,NY)
plt.xlim(0,NX)
plt.show()

enter image description here

在此处输入图片说明

NX=10
NY=20
H = np.random.rand(NY, NX)
sns.heatmap(H, xticklabels=True, yticklabels=True, annot = True)
plt.ylim(NY,0)
plt.xlim(NX,0)
plt.show()

enter image description here

在此处输入图片说明