简体   繁体   中英

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...):

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.

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

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 ). I'd recommend using 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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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