我如何在 altair 中绘制 groupby?
How do I plot the groupby in altair?
我按流派分组并尝试使用 altair 绘图,但出现以下错误。
disney_revenue = disney_movies.assign(inflation_adjusted_gross = disney_movies['inflation_adjusted_gross'].str.strip('$').str.replace(',','').astype(float))
disney_total_revenue = disney_revenue.assign(total_gross = disney_revenue['total_gross'].str.strip('$').str.replace(',','').astype(float))
disney_group = disney_total_revenue.groupby(by='genre')
chart2 = alt.Chart(disney_group, width=500, height=300).mark_circle().encode(
x='movie_title:N',
y='inflation_adjusted_gross:Q').properties(title='Total Adjusted Gross per Genre')
chart2
---------------------------------------------------------------------------
SchemaValidationError: Invalid specification
altair.vegalite.v4.api.Chart->0, validating 'type'
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f0611f2ac10> is not of type 'object'
您不能将 pandas groupby 对象传递给 alt.Chart
– 您必须传递数据框。但是如果你想可视化分组数据,你可以通过 Altair 编码语法来实现。例如,这是您尝试创建的图表版本,按流派分面:
alt.Chart(disney_total_revenue).mark_circle().encode(
x='movie_title:N',
y='inflation_adjusted_gross:Q',
facet='genre:N',
).properties(
title='Total Adjusted Gross per Genre'
)
我按流派分组并尝试使用 altair 绘图,但出现以下错误。
disney_revenue = disney_movies.assign(inflation_adjusted_gross = disney_movies['inflation_adjusted_gross'].str.strip('$').str.replace(',','').astype(float))
disney_total_revenue = disney_revenue.assign(total_gross = disney_revenue['total_gross'].str.strip('$').str.replace(',','').astype(float))
disney_group = disney_total_revenue.groupby(by='genre')
chart2 = alt.Chart(disney_group, width=500, height=300).mark_circle().encode(
x='movie_title:N',
y='inflation_adjusted_gross:Q').properties(title='Total Adjusted Gross per Genre')
chart2
---------------------------------------------------------------------------
SchemaValidationError: Invalid specification
altair.vegalite.v4.api.Chart->0, validating 'type'
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f0611f2ac10> is not of type 'object'
您不能将 pandas groupby 对象传递给 alt.Chart
– 您必须传递数据框。但是如果你想可视化分组数据,你可以通过 Altair 编码语法来实现。例如,这是您尝试创建的图表版本,按流派分面:
alt.Chart(disney_total_revenue).mark_circle().encode(
x='movie_title:N',
y='inflation_adjusted_gross:Q',
facet='genre:N',
).properties(
title='Total Adjusted Gross per Genre'
)