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

时间:2020-04-07 16:14:37

标签: python python-3.x mapping seaborn axis-labels

由于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]
}

我尝试在每个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()

enter image description here

1 个答案:

答案 0 :(得分:0)

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

在每个violinplot()

之后