y=x
关于如何在使用 Altair时对双轴图表进行分面,然后向每个图表添加一条线,有什么建议吗?挑战在于,该线y=x
需要与每个多面图表中显示的数据特定的系列比例相匹配。
链接:
下面是重现该问题的代码。
import altair as alt
from vega_datasets import data
source = data.anscombe().copy()
source['line-label'] = 'x=y'
source = pd.concat([source,source.groupby('Series').agg(x_diff=('X','diff'), y_diff=('Y','diff'))],axis=1)
source['rate'] = source.y_diff/source.x_diff
source['rate-label'] = 'rate-of-change'
base = alt.Chart().encode(
x='X:O',
)
scatter = base.mark_circle(size=60, opacity=0.30).encode(
y='Y:Q',
color=alt.Color('Series:O', scale=alt.Scale(scheme='category10')),
tooltip=['Series','X','Y']
)
line_x_equals_y = alt.Chart().mark_line(color= 'black', strokeDash=[3,8]).encode(
x=alt.X('max(X)',axis=None),
y=alt.Y('max(X)',axis=None), # note: it's intentional to set max(X) here so that X and Y are equal.
color = alt.Color('line-label') # note: the intent here is for the line label to show up in the legend
)
rate = base.mark_line(strokeDash=[5,3]).encode(
y=alt.Y('rate:Q'),
color = alt.Color('rate-label',),
tooltip=['rate','X','Y']
)
scatter_rate = alt.layer(scatter, rate, data=source)
line_x_equals_y
)scatter_rate.facet('Series',columns=2).resolve_axis( x='independent', y='independent', )
问题:Javascript 错误
alt.layer(scatter_rate, line_x_equals_y, data=source).facet('Series',columns=2).resolve_axis(
x='independent',
y='independent',
)
问题:Javascript 错误
chart_generator = (alt.layer(line_x_equals_y, scatter_rate, data = source, title=f"Series {val}").transform_filter(alt.datum.Series == val).resolve_scale(y='independent',x='independent') \
for val in source.Series.unique())
alt.concat(*(
chart_generator
), columns=2)
scatter_rate
是一个多面(按系列)双轴图表,带有适合值范围的单独刻度。
y=x
每个多面图表都包含一条从 (0,0) 到y=max(X)
各个图表的值的线。
红颜莎娜
SMILET
交互式爱情
相关分类