Python 更改 Seaborn 热图中刻度标签的旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27037241/
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
Changing the rotation of tick labels in Seaborn heatmap
提问by Artturi Bj?rk
I'm plotting a heatmap in Seaborn. The problem is that I have too many squares in my plot so the x and y labels are too close to each other to be useful. So I'm creating a list of xticks and yticks to use. However passing this list to the function rotates the labels in the plot. It would be really nice to have seaborn automatically drop some of the ticks, but barring that I would like to be able to have the yticks upright.
我正在 Seaborn 中绘制热图。问题是我的图中有太多正方形,因此 x 和 y 标签彼此太近而无用。因此,我正在创建要使用的 xticks 和 yticks 列表。但是,将此列表传递给函数会旋转图中的标签。让 seaborn 自动丢弃一些蜱真的很好,但除非我希望能够让 yticks 直立。
import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame(np.random.normal(size=40*40).reshape(40,40))
yticks = data.index
keptticks = yticks[::int(len(yticks)/10)]
yticks = ['' for y in yticks]
yticks[::int(len(yticks)/10)] = keptticks
xticks = data.columns
keptticks = xticks[::int(len(xticks)/10)]
xticks = ['' for y in xticks]
xticks[::int(len(xticks)/10)] = keptticks
sns.heatmap(data,linewidth=0,yticklabels=yticks,xticklabels=xticks)


采纳答案by Ffisegydd
seabornuses matplotlibinternally, as such you can use matplotlibfunctions to modify your plots. I've modified the code below to use the plt.yticksfunction to set rotation=0which fixes the issue.
seaborn在matplotlib内部使用,因此您可以使用matplotlib函数来修改您的绘图。我修改了下面的代码以使用该plt.yticks函数来设置rotation=0修复问题的方法。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.DataFrame(np.random.normal(size=40*40).reshape(40,40))
yticks = data.index
keptticks = yticks[::int(len(yticks)/10)]
yticks = ['' for y in yticks]
yticks[::int(len(yticks)/10)] = keptticks
xticks = data.columns
keptticks = xticks[::int(len(xticks)/10)]
xticks = ['' for y in xticks]
xticks[::int(len(xticks)/10)] = keptticks
sns.heatmap(data,linewidth=0,yticklabels=yticks,xticklabels=xticks)
# This sets the yticks "upright" with 0, as opposed to sideways with 90.
plt.yticks(rotation=0)
plt.show()


回答by Ryszard Cetnarski
You can also call the methods of heatmap object:
您还可以调用热图对象的方法:
g = sns.heatmap(data,linewidth=0,yticklabels=yticks,xticklabels=xticks)
g.set_yticklabels(g.get_yticklabels(), rotation = 0, fontsize = 8)
I am not sure why this isn't in the documentation for sns.heatmap, but the same methods are described here: http://seaborn.pydata.org/generated/seaborn.FacetGrid.html
我不确定为什么这不在 sns.heatmap 的文档中,但这里描述了相同的方法:http://seaborn.pydata.org/generated/seaborn.FacetGrid.html
I believe these methods are available to every seaborn plot object but couldn't find a general API for that.
我相信这些方法可用于每个 seaborn 绘图对象,但找不到通用的 API。

