【问题标题】:Invert axis direction Altair反转轴方向 Altair
【发布时间】:2018-09-23 19:44:48
【问题描述】:

由于某种原因,使用 altair 绘图时的 Y 轴似乎是倒置的(期望值从绘图的较低(底部)到较高(顶部))。另外,我希望能够更改滴答频率。对于旧版本,我可以使用 ticks=n_ticks 但现在看来这个参数只能取布尔值。谢谢

import altair as alt
alt.renderers.enable('notebook')

eff_metals =  pd.read_excel(filename, sheet_name='summary_eff_metals')
points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
    x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
            scale=alt.Scale(zero=False, padding=50)),
    y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
            scale=alt.Scale(zero=False, padding=1)),
    color=alt.Color('Element:N'),
)
text = points.mark_text(align='right', dx=0, dy=-5).encode(
    text='Element:N'
)
chart = alt.layer(points, text, data=eff_metals, 
                  width=600, height=300)
chart

还有图:

【问题讨论】:

    标签: python axis altair


    【解决方案1】:

    我没有你的数据,所以很难编写工作代码。

    但这是一个带有额外刻度的倒置刻度示例,类似于example scatter with tooltips 示例。在 vega 编辑器中查看here

    import altair as alt
    from vega_datasets import data
        
    iris = data.iris()
        
    alt.Chart(iris).mark_point().encode(
        x='petalWidth',
        y=alt.Y('petalLength', scale=alt.Scale(domain=[7,0]), axis=alt.Axis(tickCount=100)),
        color='species'
    ).interactive()
    

    这可能适用于您的数据:

    eff_metals =  pd.read_excel(filename, sheet_name='summary_eff_metals')
    points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
        x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
                scale=alt.Scale(zero=False, padding=50)),
        y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
                scale=alt.Scale(zero=False, padding=1, domain=[17,1])),
        color=alt.Color('Element:N'),
    )
    text = points.mark_text(align='right', dx=0, dy=-5).encode(
        text='Element:N'
    )
    chart = alt.layer(points, text, data=eff_metals, 
                      width=600, height=300)
    chart
    

    但是,我认为您的效率变量可能只是错误的type。您可以尝试将 'Efficiency:N' 替换为 `'Efficiency:Q' 可能会成功吗?

    【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多