【问题标题】:Python Altair - Bar Chart - Scale BindingPython Altair - 条形图 - 比例绑定
【发布时间】:2021-01-07 17:31:53
【问题描述】:

我想结合 Altair 文档中的两个示例来获得垂直滚动条形图的能力。一个用例是甘特图。

目前我只能水平滚动,图表内容被压缩到我定义的高度属性:

import altair as alt
from vega_datasets import data

source = data.wheat()

bars = alt.Chart(source).mark_bar().encode(
    x='wheat:Q',
    y="year:O"
)

text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=3  # Nudges text to right so it doesn't appear on top of the bar
).encode(
    text='wheat:Q'
)

selection = alt.selection_interval(bind='scales')
(bars + text).properties(height=300).add_selection(
    selection
)

如何配置图表以保留原始比例并允许在定义的高度内滚动?

谢谢!

【问题讨论】:

    标签: python charts scroll scaling altair


    【解决方案1】:

    分类编码,例如序数 (":O") 和名义 (":N") 不能有比例限制选择。要使垂直轴交互,您应该使 y 编码量化。例如:

    bars = alt.Chart(source).mark_bar(orient='horizontal').encode(
        x='wheat:Q',
        y='year:Q',
    )
    

    【讨论】:

    • 谢谢你,@jakevdp - 关于这个主题的大多数答案都来自你,所以我为你的努力喝彩!
    猜你喜欢
    • 2019-10-04
    • 2019-03-18
    • 2020-04-23
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多