Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

Python Altair - Bar Chart - Scale Binding

I would like to combine two examples from Altair documentation to gain the ability to scroll through a bar-chart vertically. One use case of this would be a Gantt chart.

Presently I only gain the ability to scroll horizontally, with the chart content being squashed to the height property that I define:

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
)

How can I configure the chart to retain the original proportions and allow scrolling within the defined height?

Thank you!

question from:https://stackoverflow.com/questions/65617233/python-altair-bar-chart-scale-binding

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Categorical encodings, such as ordinal (":O") and nominal (":N") cannot have scale-bound selections. To make the vertical axis interactive, you should make the y encoding quantitative. For example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...