如何将海洋图上的整数轴标签更改为我自己的字符串?

问题描述 投票:0回答:1

由于seaborn小提琴只使用数字,他将有意投票转换为整数。可以在标签上贴上正确的标签吗?

我做了

def code(x):
    if x == (df['Intention_vote_2021'][0]):
        return 0
    elif x == (df['Intention_vote_2021'][1]):
        return 1
    elif x == (df['Intention_vote_2021'][2]):
        return 2
    elif x == (df['Intention_vote_2021'][3]):
        return 3

df['ascii'] = [code(x) for x in df['Intention_vote_2021']] # crecion de una nueva columna objetivo

import matplotlib.pyplot as plt
import seaborn as sns

sns.set()
plt.rcParams["figure.figsize"] = (20,8)
plt.rcParams["axes.titlesize"] = 18
plt.rcParams["axes.labelsize"] = 16
plt.rcParams["xtick.labelsize"] = 12
plt.rcParams["ytick.labelsize"] = 12

# Create violinplot
plt.subplot(121)
v1 = sns.violinplot(x = "Groupe_dage", y="ascii", data=df)
v1.axes.set_title("Voting intention according to age", fontsize=20)

plt.subplot(122)
v2 = sns.violinplot(x = "Revenu_mensuel", y="ascii", data=df)
v2.axes.set_title("Voting intention according to income", fontsize=20)

# Show the plot
plt.show()

它返回:

introducir la descripción de la imagen aquí

而且我想使用来自数据框的实际标签:

{ 0: df['Intention_vote_2021'][0],
  1: df['Intention_vote_2021'][1],
  2: df['Intention_vote_2021'][2],
  3: df['Intention_vote_2021'][3]
}
python python-3.x mapping seaborn axis-labels
1个回答
0
投票

plt.yticks([0,1,2,3], df['Intention_vote_2021'])

每个violinplot()之后

© www.soinside.com 2019 - 2024. All rights reserved.