如何创建分组线型图

2024-05-18 17:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个线图,x轴是年,y轴是avo_sales.groupby(['year','type'])['type'].count(),这意味着每年每种类型的计数,色调是类型。我做不到。它说TypeError: object of type 'int' has no len()

这些数据就是我想要绘制的

avo_sales.groupby(['year','type'])['type'].count()
Out[17]: 
year  type        
2015  conventional    2790
      organic         2807
2016  conventional    2808
      organic         2808
2017  conventional    2862
      organic         2860
2018  conventional     648
      organic          648
Name: type, dtype: int64

这是我的密码

plt.figure()
sns.lineplot(x= avo_sales.year,y=avo_sales.groupby(['year','type'])['type'].count(),hue = avo_sales.type)

Tags: of类型objecttypecount色调year计数
1条回答
网友
1楼 · 发布于 2024-05-18 17:51:48

你可以尝试以下方法

以下代码适用于熊猫>;=0.25

pd.__version__使用这些来显示版本


df = pd.read_csv('avocado.csv')
df = df.groupby(['year','type']).agg(count=('type', 'count'))
# print df here to see how the columns are displaying
df = df.reset_index()
# print df here to see the difference

sns.lineplot(x="year", y="count", hue='type', data=df)

它将给出以下输出 enter image description here

相关问题 更多 >

    热门问题