簡體   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