Поскольку морские скрипки используют только цифры, он превратил намеренные голоса в целые числа. Можно ли будет поставить на них правильные ярлыки?
Я сделал
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()
И он вернул:
И я хотел бы использовать фактические метки, которые приходят из фрейма данных:
{ 0: df['Intention_vote_2021'][0],
1: df['Intention_vote_2021'][1],
2: df['Intention_vote_2021'][2],
3: df['Intention_vote_2021'][3]
}
Я пытался добавить plt.yticks([0,1,2,3], df['Intention_vote_2021'])
после каждого sns.violinplot()
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)
plt.yticks([0,1,2,3], df['Intention_vote_2021'])
v1.axes.set_title("Voting intention according to age", fontsize=20)
plt.subplot(122)
v2 = sns.violinplot(x = "Revenu_mensuel", y="ascii", data=df)
plt.yticks([0,1,2,3], df['Intention_vote_2021'])
v2.axes.set_title("Voting intention according to income", fontsize=20)
# Show the plot
plt.show()