1

遇到一个问题,如何使用字典重命名lineplot中现有的xticklabels?部分代码:

zodiac_order = ['Aries', 'Taurus', 'Gemini', 'Cancer',  'Leo', 'Virgo', 'Libra', 'Scorpio','Sagittarius', 'Capricorn', 'Aquarius', 'Pisces']    # df_one here is DataFrame
zodiac_counts = df_one["Zodiac"].value_counts()
plt.figure(figsize=(14,6))
zodiac = sns.lineplot(data=zodiac_counts, marker='o', color='mediumvioletred')
zodiac.yaxis.grid(True)
[plt.gca().spines[loc].set_visible(False) for loc in ['top', 'right']]
zodiac.set(xticklabels=zodiac_order) # made list to be in order according to real list of zodiacs

我想改变xticks 标签的语言,所以有字典

zodiacs_lt = {'Aries': 'Avinas', 
              'Taurus': 'Jautis', 
              'Gemini': 'Dvyniai', 
              'Cancer': 'Vėžys', 
              'Leo': 'Liūtas', 
              'Virgo': 'Mergelė', 
              'Libra': 'Svarstyklės', 
              'Scorpio': 'Skorpionas', 
              'Sagittarius': 'Šaulys', 
              'Capricorn': 'Ožiaragis', 
              'Aquarius': 'Vandenis', 
              'Pisces': 'Žuvys'
             }

线图本身

4

1 回答 1

0

我假设您的数据位于这样的数据框中df

zodiac_order = ['Aries', 'Taurus', 'Gemini', 'Cancer',  'Leo', 'Virgo', 'Libra', 'Scorpio','Sagittarius', 'Capricorn', 'Aquarius', 'Pisces']
df = pd.DataFrame({'signs': zodiac_order,
                   'values': [6, 1, 6, 5, 6, 8, 1, 6, 4, 1, 5, 1]})

输出:

          signs  values
0         Aries       6
1        Taurus       1
2        Gemini       6
3        Cancer       5
4           Leo       6
5         Virgo       8
6         Libra       1
7       Scorpio       6
8   Sagittarius       4
9     Capricorn       1
10     Aquarius       5
11       Pisces       1

您可以使用十二生肖的翻译名称向此数据框添加一个新列:

zodiacs_lt = {'Aries': 'Avinas',
              'Taurus': 'Jautis',
              'Gemini': 'Dvyniai',
              'Cancer': 'Vėžys',
              'Leo': 'Liūtas',
              'Virgo': 'Mergelė',
              'Libra': 'Svarstyklės',
              'Scorpio': 'Skorpionas',
              'Sagittarius': 'Šaulys',
              'Capricorn': 'Ožiaragis',
              'Aquarius': 'Vandenis',
              'Pisces': 'Žuvys'}
df['new_signs'] = list(map(lambda x: zodiacs_lt[x], df['signs']))

输出:

          signs  values    new_signs
0         Aries       6       Avinas
1        Taurus       1       Jautis
2        Gemini       6      Dvyniai
3        Cancer       5        Vėžys
4           Leo       6       Liūtas
5         Virgo       8      Mergelė
6         Libra       1  Svarstyklės
7       Scorpio       6   Skorpionas
8   Sagittarius       4       Šaulys
9     Capricorn       1    Ožiaragis
10     Aquarius       5     Vandenis
11       Pisces       1        Žuvys

然后你可以继续使用new_signs列进行绘图:

plt.figure(figsize=(14,6))

zodiac = sns.lineplot(data=df, x='new_signs', y='values', marker='o', color='mediumvioletred', sort=False)
zodiac.yaxis.grid(True)
[plt.gca().spines[loc].set_visible(False) for loc in ['top', 'right']]

plt.show()

这使:

在此处输入图像描述

注意
我删除了该行

zodiac.set(xticklabels=zodiac_order) # made list to be in order according to real list of zodiacs

这可能会导致错误,我实现了与sort=False参数相同的功能sns.lineplot

于 2020-07-22T09:24:42.933 回答