python - 我的 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)
我想要的是为每个 pokemons 类型的每个统计数据制作一些 swarmplots,hue=Legendary。我想想象一下传说中的口袋妖怪是如何分布的。我已经做了没有色调的swarmplots。首先,我需要融化数据框:
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:
所以我试着这样做:
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: boolean index did not match indexed array along dimension 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-3.x - 如何通过列值范围的caparison提取行
- c - 为什么为 scanf 输入设置多个句点会跳过下一个 scanf 函数?
- mysql - SQL查询查找居住在同一城市的雇员姓名
- jquery - 解决 Odoo 12 中移动 Safari 浏览器上的 JS 语法错误
- hyperledger-fabric - 如何将cryptogen工具生成的密钥与fabric CA同步
- vue.js - 有没有办法使用 Vuetify 嵌入 360 度图像?
- processing - 如何刷新窗口但仍保留以前绘制的对象
- java - 为什么 Java 泛型会有这种行为?
- javascript - 如何在 2.0 中获取 vuetify 数据表选择的 itemsPerPage?
- go - 如何修复'(1<<100)*0.1 和 (1<<100)/10'