在Seaborn热图中更改刻度标签的旋转 [英] Changing the rotation of tick labels in Seaborn heatmap

查看:1509
本文介绍了在Seaborn热图中更改刻度标签的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Seaborn中绘制热图.问题是我的绘图中有太多正方形,因此x和y标签彼此之间太靠近而无法使用.因此,我正在创建要使用的xticks和yticks的列表.但是,将此列表传递给函数会旋转绘图中的标签.拥有seaborn自动掉落某些壁虱的确很棒,但是除非我希望能够将yticks直立.

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.

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)

推荐答案

seaborn在内部使用matplotlib,因此您可以使用matplotlib函数来修改图.我已经修改了下面的代码,以使用 plt.yticks 函数来设置rotation=0解决该问题.

seaborn uses matplotlib internally, as such you can use matplotlib functions to modify your plots. I've modified the code below to use the plt.yticks function to set rotation=0 which fixes the issue.

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()

这篇关于在Seaborn热图中更改刻度标签的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆