简体   繁体   English

在Seaborn绘图中旋转Y轴分类标签?

[英]Rotate Y-axis categorical label in Seaborn plot?

To borrow some default code with default libraries (extra helpful that you have to load the data explicitly, instead of having it polluting globals like in R...): 要借用默认库的一些默认代码(特别有用,您必须显式加载数据,而不是像R ...那样污染全局变量):

sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(y="day", x="total_bill", data=tips)

This produces the following image: 这将产生以下图像:

在此处输入图片说明

I want to rotate the day of the week labels 90 degrees such that the baseline for those labels matches the word day , also on the Y axis. 我想将星期几标签旋转90度,以使这些标签的基线与单词day (也在Y轴上)匹配。

I've looked at the answers already here, but they seem primarily to deal with (1) the X axis and (2) the first-order Y-axis label (here, the day label) rather than the Y-axis sub-labels (here, the days of the week). 我已经在这里查看了答案,但它们似乎主要处理(1)X轴和(2)一阶Y轴标签(此处为day标签),而不是Y轴子字段。标签(这里是星期几)。

Most stackoverflow answers would recommend using ax.set_yticklabels(rotation = 90) , while this does work, it also requires you to provide the positional parameter labels (failing to provide this will give you a TypeError ). 大多数stackoverflow答案都建议使用ax.set_yticklabels(rotation = 90) ,尽管这样做确实可行,但它还需要您提供位置参数labels (如果不提供,则会提供TypeError )。 I'd recommend using plt.yticks(rotation = 90) . 我建议使用plt.yticks(rotation = 90) This is how it'd look: 它是这样的:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(y="day", x="total_bill", data=tips)
plt.yticks(rotation = 90)
plt.show(ax)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM