繁体   English   中英

如何在 Altair 的多面(或类似)双轴图表中添加特定于系列的线 y=x?

[英]How to add a series specific line y=x to a faceted (or similar) dual-axis chart in Altair?

关于如何使用 Altair 对双轴图表进行刻面,然后在y=x处向每个图表添加一条线的任何建议? 挑战在于线y=x需要匹配特定于每个分面图表中显示的数据的系列的比例。

链接:

  1. 有关方面的 altair github 问题线程
  2. 轴显示上的 altair github 问题线程

下面是重现该问题的代码。

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)

目标

  1. scatter_rate是一个分面(按系列)双轴图表,带有适用于值范围的单独刻度。
  2. 每个分面图表都包含一条线y=x ,它从单个图表的 (0,0) 到y=max(X)值。

您可以通过正常创建图层并在图层图表上调用facet()方法来完成此操作。 唯一的要求是所有层共享相同的源数据; 无需手动构建 facet,也无需在当前版本的 Altair 中对 facet 进行后期数据绑定:

import altair as alt
from vega_datasets import data
import pandas as pd

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'] = 'line y=x'

source_linear = source.groupby(by=['Series']).agg(x_linear=('X','max'), y_linear=('X', 'max')).reset_index().sort_values(by=['Series'])

source_origin = source_linear.copy()
source_origin['y_linear'] = 0
source_origin['x_linear'] = 0

source_linear = pd.concat([source_origin,source_linear]).sort_values(by=['Series'])

source = source.merge(source_linear,on='Series').drop_duplicates()

scatter = alt.Chart(source).mark_circle(size=60, opacity=0.60).encode(
    x='X:Q',
    y='Y:Q',
    color='Series:N',
    tooltip=['X','Y','rate']
)

rate = alt.Chart(source).mark_line(strokeDash=[5,3]).encode(
    x='X:Q',
    y='rate:Q',
    color = 'rate-label:N'
)

line_plot = alt.Chart(source).mark_line(color= 'black', strokeDash=[3,8]).encode(
    x=alt.X('x_linear', title = ''),
    y=alt.Y('y_linear', title = ''),
    shape = alt.Shape('rate-label', title = 'Break Even'),
    color = alt.value('black')
)

alt.layer(scatter, rate, line_plot).facet(
    'Series:N'
).properties(
    columns=2
).resolve_scale(
    x='independent',
    y='independent'
)

在此处输入图片说明

此解决方案在y=x为每个图表上的数据构建所需的线; 但是,点在合并步骤中重复,我不确定如何添加双轴速率。

获取数据

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'] = 'line y=x'

创建 Y=X 线数据

source_linear = source.groupby(by=['Series']).agg(x_linear=('X','max'), y_linear=('X', 'max')).reset_index().sort_values(by=['Series'])

source_origin = source_linear.copy()
source_origin['y_linear'] = 0
source_origin['x_linear'] = 0

source_linear = pd.concat([source_origin,source_linear]).sort_values(by=['Series'])

合并线性数据

source = source.merge(source_linear,on='Series').drop_duplicates()

构建图表

scatter = alt.Chart().mark_circle(size=60, opacity=0.60).encode(
    x=alt.X('X', title='X'),
    y=alt.Y('Y', title='Y'),
    #color='year:N',
    tooltip=['X','Y','rate']
)

line_plot = alt.Chart().mark_line(color= 'black', strokeDash=[3,8]).encode(
    x=alt.X('x_linear', title = ''),
    y=alt.Y('y_linear', title = ''),
    shape = alt.Shape('rate-label', title = 'Break Even'),
    color = alt.value('black')
)

手动分面图

chart_generator =  (alt.layer(scatter, line_plot, data = source, title=f"{val}: Duplicated Points w/ Line at Y=X").transform_filter(alt.datum.Series == val) \
             for val in source.Series.unique())

组合图表

chart = alt.concat(*(
    chart_generator
), columns=3)

chart.display()

在此处输入图片说明

该解决方案包括速率,但不是一个双轴,一个轴是Y ,另一个轴是rate

import altair as alt
from vega_datasets import data
import pandas as pd

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'
source['line-label'] = 'line y=x'

source_linear = source.groupby(by=['Series']).agg(x_linear=('X','max'), y_linear=('X', 'max')).reset_index().sort_values(by=['Series'])

source_origin = source_linear.copy()
source_origin['y_linear'] = 0
source_origin['x_linear'] = 0

source_linear = pd.concat([source_origin,source_linear]).sort_values(by=['Series'])

source = source.merge(source_linear,on='Series').drop_duplicates()

scatter = alt.Chart(source).mark_circle(size=60, opacity=0.60).encode(
    x=alt.X('X', title='X'),
    y=alt.Y('Y', title='Y'),
    color='Series:N',
    tooltip=['X','Y','rate']
)

line_plot = alt.Chart(source).mark_line(color= 'black', strokeDash=[3,8]).encode(
    x=alt.X('x_linear', title = ''),
    y=alt.Y('y_linear', title = ''),
    shape = alt.Shape('line-label', title = 'Break Even'),
    color = alt.value('black')
)

rate =  alt.Chart(source).mark_line(strokeDash=[5,3]).encode(
    x=alt.X('X', axis=None, title = 'X'),
    y=alt.Y('rate:Q'),
    color = alt.Color('rate-label',),
    tooltip=['rate','X','Y']
)

alt.layer(scatter, line_plot, rate).facet(
    'Series:N'
).properties(
    columns=2
).resolve_scale(
    x='independent',
    y='independent'
).display()

在此处输入图片说明

暂无
暂无

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

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