繁体   English   中英

Altair 分层多面图表上的滚动平均值

[英]Rolling average on a layered faceted chart in Altair

我成功地让图层在多面图表中工作,并让滚动平均值在分层图表中工作。 我现在想将两者结合起来,即在分层多面图表中具有滚动平均值。

直观地将两者结合起来会给我一个错误-

Javascript Error: Cannot read property 'concat' of undefined
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

代码(给出上述错误):

# Data Preparation
df = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
idf = df[df['Country/Region'] == 'India']
idf = idf[df.columns[4:]]
idf = idf.T
idf = idf.reset_index()
idf.columns = ['day', 'case']
idf['country'] = 'india'

gdf = df[df['Country/Region'] == 'Germany']
gdf = gdf[df.columns[4:]]
gdf = gdf.T
gdf = gdf.reset_index()
gdf.columns = ['day', 'case']
gdf['country'] = 'germany'

fdf = pd.concat([idf,gdf])

# Charting
a = alt.Chart().mark_bar(opacity=0.5).encode(
    x='day:T',
    y='case:Q'
)

c = alt.Chart().mark_line().transform_window(
    rolling_mean='mean(case:Q)',
    frame=[-7, 0]
).encode(
    x='day:T',
    y='rolling_mean:Q'
)

alt.layer(a, c, data=fdf).facet(alt.Column('country', sort=alt.EncodingSortField('case', op='max', order='descending')))

如果您删除transform_window并将y='rolling_mean:Q'替换为y='case:Q' ,您将获得分层多面图。 正是这张图表,我想要一个 7 天的滚动平均值。

你应该用这个替换你的 window 变换:

.transform_window(
    rolling_mean='mean(case)',
    frame=[-7, 0],
    groupby=['country']
)

您的原始转换存在两个问题:

  • 类型简写仅用于编码,从不用于转换。 当您编写mean(case:Q)时,您指定了名为"case:Q"的字段的滚动平均值,该字段不存在。

  • 由于您按国家/地区分面,因此在计算滚动平均值时需要按国家/地区分组。

结果如下所示: 在此处输入图像描述

尝试通过 sort=[{'field': 'date'}] https://vega.github.io/vega-lite/docs/window.html#cumulative-frequency-distribution 使用 transform_window

或:https://altair-viz.github.io/gallery/scatter_marginal_hist.html

https://altair-viz.github.io/gallery/layered_chart_with_dual_axis.html#layered-chart-with-dual-axis

https://altair-viz.github.io/gallery/parallel_coordinates.html#parallel-coordinates-example

import altair as alt
from vega_datasets import data

source = data.iris()

alt.Chart(source).transform_window(
    index='count()'
).transform_fold(
    ['petalLength', 'petalWidth', 'sepalLength', 'sepalWidth']
).mark_line().encode(
    x='key:N',
    y='value:Q',
    color='species:N',
    detail='index:N',
    opacity=alt.value(0.5)
).properties(width=500)

https://altair-viz.github.io/user_guide/compound_charts.html?highlight=repeat#horizontal

import altair as alt
from vega_datasets import data

iris = data.iris.url

chart1 = alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    height=300,
    width=300
)

chart2 = alt.Chart(iris).mark_bar().encode(
    x='count()',
    y=alt.Y('petalWidth:Q', bin=alt.Bin(maxbins=30)),
    color='species:N'
).properties(
    height=300,
    width=100
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM