我有此数据集:https://www.kaggle.com/abcsds/pokemon/download。我加载了它并进行了一些更改:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

pokemons=pd.read_csv('../input/pokemon/Pokemon.csv')

del pokemons['Type 2']
pokemons.rename(columns={'Type 1':'Type'},inplace=True)


我想要的是用hue = Legendary为每个宠物小精灵类型的每个属性制作一些变形图。我想想像一下传说中的宠物小精灵的位置。我已经做了一些没有色调的小样。首先,我需要融合数据框:

pok_melt=pd.melt(pokemons,id_vars=['Name','Type','Legendary'],value_vars=['HP','Defense','Attack','Sp. Atk','Sp. Def','Speed'])
pok_melt.head()


然后,为swarmplots编写代码(在某一时刻,我需要按字母顺序对另一种绘图进行排序的类型名称,这就是为什么对其进行排序的原因):

list_types=pokemons['Type'].unique().tolist()
list_types.sort()
list_types

plt.figure(figsize=(17,22))
k=1
for i in list_types:
    plt.subplot(6,3,k)
    k=k+1
    sns.swarmplot(x=pok_melt.variable,y=pok_melt[pok_melt.Type==i].value,palette='gist_stern')
    plt.title(i)
    plt.xlabel('')


这些是一些swarmplots:

python - 我的Swarmplot中的色调有什么问题?-LMLPHP

所以我尝试这样做:

plt.figure(figsize=(17,22))
k=1
for i in list_types:
    plt.subplot(6,3,k)
    k=k+1
    sns.swarmplot(x=pok_melt.variable,y=pok_melt[pok_melt.Type==i].value,palette='gist_stern',
    hue=pok_melt.Legendary)
    plt.title(i)
    plt.xlabel('')


我得到这个错误:IndexError:布尔索引与沿维度0的索引数组不匹配;维度为69,但相应的布尔维度为800

最佳答案

Legendary参数一样过滤列y

plt.figure(figsize=(17,22))
k=1
for i in list_types:
    plt.subplot(6,3,k)
    k=k+1
    sns.swarmplot(x=pok_melt.variable,
                  y=pok_melt[pok_melt.Type==i].value,
                  hue=pok_melt[pok_melt.Type==i].Legendary,
                  palette='gist_stern')
    plt.title(i)
    plt.xlabel('')


或者更好的方法是只对变量df过滤一次,并将列df['value']分配给y,将df['Legendary']分配给hue

plt.figure(figsize=(17,22))
k=1
for i in list_types:
    plt.subplot(6,3,k)
    k=k+1
    df = pok_melt.loc[pok_melt.Type==i]

    sns.swarmplot(x=pok_melt.variable,
                  y=df['value'],
                  hue=df['Legendary'],
                  palette='gist_stern')
    plt.title(i)
    plt.xlabel('')

关于python - 我的Swarmplot中的色调有什么问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58132415/

10-13 09:13