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

时间:2019-05-12 12:21:05

标签: python-3.x matplotlib axis-labels violin-plot

我做了一个 violinplot ,并想重命名x标签。

onClick="toggleColor()"

这是输出:

enter image description here

我想要拥有的是,而不是我想要的 第1周 的1个,而不是44个我想要的 第2周 ,直到 第10周 )。

谢谢大家

1 个答案:

答案 0 :(得分:0)

您正在寻找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()

enter image description here