Altair-viz 重复和转换
Altair-viz repeat and transform
有没有办法使用重复图表使用的编码来应用数据转换或过滤器?
如果我对 docs 的理解是正确的,那么这似乎不是直接可能的:
Currently repeat can only be specified for rows and column (not, e.g., for layers) and the target can only be encodings (not, e.g., data transforms) but there is discussion within the Vega-Lite community about making this pattern more general in the future.
解决这个问题的好方法是什么?例如下面,假设我只想绘制 y>0
的点(或者可能是另一个变换,我不想只在 y 轴上缩放)。有没有一种方法可以使用 repeat 目标应用类似第 0 行的内容(如在 #1 中尝试的那样,失败并显示 TypeError: '>' not supported between instances of 'RepeatRef' and 'float'
)?
import altair as alt
import numpy as np
import pandas as pd
x = np.arange(100)
source = pd.DataFrame({
'x': x,
'f': np.sin(x / 5),
'g': np.cos(x / 3),
})
alt.Chart(source).mark_line().encode(
alt.X('x', type='quantitative'),
alt.Y(alt.repeat('column'), type='quantitative'),
).transform_filter(
# alt.datum.f >= 0. #0 Works, but would like to use f or g depending on the plotted variable
alt.repeat('column') > 0. #1 ERROR HERE
).repeat(
column=['f', 'g']
)
无法在转换中引用重复字段。解决这个问题的最佳方法是通过串联构建图表;例如:
alt.hconcat(*(
alt.Chart(source).mark_line().encode(
alt.X('x', type='quantitative'),
alt.Y(col, type='quantitative'),
).transform_filter(
alt.datum[col] >= 0
)
for col in ['f', 'g']
))
有没有办法使用重复图表使用的编码来应用数据转换或过滤器?
如果我对 docs 的理解是正确的,那么这似乎不是直接可能的:
Currently repeat can only be specified for rows and column (not, e.g., for layers) and the target can only be encodings (not, e.g., data transforms) but there is discussion within the Vega-Lite community about making this pattern more general in the future.
解决这个问题的好方法是什么?例如下面,假设我只想绘制 y>0
的点(或者可能是另一个变换,我不想只在 y 轴上缩放)。有没有一种方法可以使用 repeat 目标应用类似第 0 行的内容(如在 #1 中尝试的那样,失败并显示 TypeError: '>' not supported between instances of 'RepeatRef' and 'float'
)?
import altair as alt
import numpy as np
import pandas as pd
x = np.arange(100)
source = pd.DataFrame({
'x': x,
'f': np.sin(x / 5),
'g': np.cos(x / 3),
})
alt.Chart(source).mark_line().encode(
alt.X('x', type='quantitative'),
alt.Y(alt.repeat('column'), type='quantitative'),
).transform_filter(
# alt.datum.f >= 0. #0 Works, but would like to use f or g depending on the plotted variable
alt.repeat('column') > 0. #1 ERROR HERE
).repeat(
column=['f', 'g']
)
无法在转换中引用重复字段。解决这个问题的最佳方法是通过串联构建图表;例如:
alt.hconcat(*(
alt.Chart(source).mark_line().encode(
alt.X('x', type='quantitative'),
alt.Y(col, type='quantitative'),
).transform_filter(
alt.datum[col] >= 0
)
for col in ['f', 'g']
))