3 回答
TA贡献1842条经验 获得超12个赞
您可以通过正常创建图层并调用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'
)
TA贡献1796条经验 获得超4个赞
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()
TA贡献1712条经验 获得超3个赞
该解决方案包括速率,但不是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()
添加回答
举报