【问题标题】:Altering the X-axis in Altair在 Altair 中更改 X 轴
【发布时间】:2021-12-29 19:38:53
【问题描述】:

我想用选择器填充图表,如下例所示。关于如何让它在多面图表中工作的任何提示?

np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.rand(8, 4), 0).round(2),
                    columns=['A', 'B', 'C', 'D'], index=pd.RangeIndex(8, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
    
xRange= pd.DataFrame(np.linspace(min(source['x']), max(source['x']), num=100), columns=['x'])

pts = alt.selection_multi(fields=['x'], nearest=True, on='click',empty='none')
    
# The basic line
main = alt.Chart(source).mark_line(interpolate='basis').encode(
    x='x:Q',
    y='y:Q',
).transform_filter(
   alt.FieldEqualPredicate(field='category', equal='A')
)

line = alt.Chart(source).mark_line(color='Maroon').encode(
    x='x:Q',
    y='y:Q',
).transform_filter(
   alt.FieldEqualPredicate(field='category', equal='B')
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(xRange).mark_rule(size=2).encode(
    x='x:Q',
    #y='y:Q',
    #opacity=alt.value(0.4),
    opacity = alt.condition(pts, alt.value(1.0), alt.value(0.2))
).add_selection(pts)

position = alt.Chart(xRange).mark_text(
    align='right', dy=140, dx=-8, fontSize=14).encode(
    x=alt.X('x'),
    text=alt.Text('x',format='.1f')
).transform_filter(pts)
        
alt.vconcat(
    main + selectors + position,
    line + selectors + position
)

但理想情况下使用 facet,但我还没有找到一种方法,您只能使用单个 DataFrame/源。有没有办法使用 alt.sequence 的 impute 在 x 轴上生成额外的点?

pts = alt.selection_multi(fields=['x'], nearest=True, on='click',empty='none')

# The basic line
line = alt.Chart().mark_line(interpolate='basis').encode(
    x='x:Q',
    y='y:Q',
)

# Transparent rules across the chart. 
rules = alt.Chart().mark_rule(size=2).encode(
    x='x:Q',
    opacity = alt.condition(pts, alt.value(1.0), alt.value(0.3))
).add_selection(pts)

text = alt.Chart().mark_text(
    align='right', dy=140, dx=-8, fontSize=14).encode(
    x=alt.X('x'),
    text=alt.Text('x',format='.1f')
).transform_filter(pts)
        
alt.layer(line, rules, text, data=source).facet(
    'category:N',
    columns=2
)

【问题讨论】:

    标签: facet altair


    【解决方案1】:

    您可以使用序列生成器。它与您已经拥有的几乎相同:

    import numpy as np
    import pandas as pd
    import altair as alt 
    
    np.random.seed(42)
    source = pd.DataFrame(np.cumsum(np.random.rand(8, 4), 0).round(2),
                        columns=['A', 'B', 'C', 'D'], index=pd.RangeIndex(8, name='x'))
    source = source.reset_index().melt('x', var_name='category', value_name='y')
        
    # xRange= pd.DataFrame(np.linspace(min(source['x']), max(source['x']), num=100), columns=['x'])
    xRange = alt.sequence(0, 7.1, 0.1, as_='x')
    
    pts = alt.selection_multi(fields=['x'], nearest=True, on='mouseover',empty='none')
    
    # The basic line
    line = alt.Chart().mark_line(interpolate='linear').encode(
        x='x:Q',
        y='y:Q',
    )
    
    # Transparent rules across the chart. 
    rules = alt.Chart(xRange).mark_rule(size=2).encode(
        x='x:Q',
        opacity = alt.condition(pts, alt.value(1.0), alt.value(0.3))
    ).add_selection(pts)
    
    text = alt.Chart(xRange).mark_text(
        align='right', dy=140, dx=-8, fontSize=14).encode(
        x=alt.X('x:Q'),
        text=alt.Text('x:Q',format='.1f')
    ).transform_filter(pts)
            
    alt.layer(line, rules, text, data=source).facet(
        'category:N',
        columns=2
    )
    

    【讨论】:

    • 谢谢你,就像一个魅力。在使用 facet 时,我已经放弃尝试合并两个来源,因为它明确说明它不起作用。
    猜你喜欢
    • 2016-01-16
    • 2022-01-12
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多