如何重命名小提琴图中的 x 标签?

How to rename the x-labels in a violinplot?

我制作了一个 violinplot 并想重命名 x-labels .

  ax = sns.violinplot(x="Week_Number", y="Ammonia", data=Res)

这是输出:

我想要的是,而不是 1 我想要 第 1 周,而不是我想要的 44 第 2 周第 10 周 52.

谢谢大家

您正在查找 set_xticklabels 属性 (doc)。要应用此功能,您需要有轴。 set_yticklabels 的 y 标签也一样。

此处代码改编自Seaborn examples:

# Import modules
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

# Create your list of labels
week_list = ["Week_" + str(i) for i in range(1, 10)]
# ['Week_1', 'Week_2', 'Week_3', 'Week_4', 'Week_5', 'Week_6', 'Week_7', 'Week_8', 'Week_9']

fig = plt.figure()                  # Create a new figure for getting axis
ax = fig.add_subplot(111)           # Get the axis

# Create a random dataset across several variables
rs = np.random.RandomState(0)
n, p = 40, 8
d = rs.normal(0, 2, (n, p))
d += np.log(np.arange(1, p + 1)) * -5 + 10

# Use cubehelix to get a custom sequential palette
pal = sns.cubehelix_palette(p, rot=-.5, dark=.3)

# Show each distribution with both violins and points
sns.violinplot(data=d, palette=pal, inner="points")

week_list = ["Week_" + str(i) for i in range(1,10)]

# Set the x labels
ax.set_xticklabels(week_list)

# Show figure
plt.show()