python - 我的群图中的色调有什么问题?

标签 python pandas data-visualization seaborn swarmplot

我有这个数据集: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)

我想要的是为每个神奇宝贝类型的每个统计数据制作一些群图,色调=传奇。我想想象一下传说中的神奇宝贝的位置。我已经做了没有色调的群图。首先,我需要融化数据框:

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

然后,群图的代码(在某一时刻,我需要按字母顺序为另一个图排序的类型名称,这就是它们排序的原因):

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('')

这些是一些群图:

enter image description here

所以我尝试这样做:

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: bool 索引与维度 0 上的索引数组不匹配;维度为 69,但对应的 bool 维度为 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'] 分配给 ydf[ '传奇']色调:

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 - 我的群图中的色调有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58132415/

相关文章:

python - Pandas 字典向量化查找

python - 如何使用 python 3 让数据沿线显示?

javascript - 来自 JSON 的 D3 节点和链接以及嵌套子数组

python - 在 python 中使用变量从字符串切片的末尾索引它不提供对最后一个元素的通用访问

python - 如何从 Python 中的类内部访问类方法

python - 为什么类型的 Union 不能解析为 Python 中的受约束泛型类型?

python - Pandas : filter the rows based on a column containing lists

python - 从 CSV 导入格式错误转换日期时间

python - 具有扩展列的 Pandas GroupBy 函数

python - 图表未在 Web 浏览器中显示 |阴谋 | Python