【发布时间】:2021-03-25 21:38:43
【问题描述】:
为什么我的 Seaborn 条形图中的 y 值没有正确求和?下表包含预期值,您可以在下图中看到它们不匹配。代码在最后。这不是 Seaborn bar plot y axis has different values than expected 的重复,因为那里的问题与日期时间格式有关。
df.groupby(['Category', 'Sub-Category'])['Profit'].sum()
Category Sub-Category
Furniture Bookcases 42.4
Tables 301.3
Supplies Labels 285.5
Paper 82.7
Technology Copiers -42.1
Phones 155.0
g = sns.FacetGrid(data=df, col='Category', sharex=False, sharey=False)
g.map_dataframe(sns.barplot, x='Sub-Category', y='Profit', ci=None)
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
### Create dummy data
cat = [ 'Furniture','Supplies','Furniture',
'Supplies','Furniture','Technology',
'Technology','Technology','Supplies',
'Furniture','Supplies','Furniture',
'Supplies','Furniture', 'Technology',
'Technology','Technology','Supplies',
]
scat = ['Bookcases','Labels','Tables',
'Labels','Bookcases','Copiers',
'Copiers','Phones','Labels',
'Bookcases','Paper','Tables',
'Paper','Tables', 'Phones',
'Phones','Copiers','Paper',
]
prof = [41.1,219.5,6.8,
-383,2.5, 12.1,
-71.2,16,449,
-1.2,9,313,
92,-18.5,132,
7,17,-18.3,
]
df = pd.DataFrame(list(zip(cat, scat, prof)),
columns =['Category', 'Sub-Category', 'Profit']
)
print(df.groupby(['Category', 'Sub-Category'])['Profit'].sum())
g = sns.FacetGrid(data=df, col='Category', sharex=False, sharey=False)
g.map_dataframe(sns.barplot, x='Sub-Category', y='Profit', ci=None)
plt.show()
顺便说一句:我也尝试了 catplot,但我无法关闭空类别。例如,“纸”仍显示在“家具”类别的 x 轴上。我使用了g = sns.catplot(data=df, x='Sub-Category', y='Profit', col='Category', kind='bar', ci=None,) 并尝试添加sharex=False 或facet_kws={"dropna": True},但这不起作用。并且它仍然显示与上例相同的 y 轴值。
【问题讨论】:
-
你为什么期望它们被求和?
标签: python pandas matplotlib seaborn