benjum / idre-spring21-python-data-viz-2

13 stars 3 forks source link

Altair data-driven inputs #1

Open emonson opened 3 years ago

emonson commented 3 years ago

Hey Ben,

My name is Eric Monson, and I work in the Duke Libraries' Center for Data and Visualization Sciences. I really enjoyed watching your recent data visualization in Python workshop videos! I learned a few helpful things, and I'm always curious about how others teach their material.

I'm writing just in case you didn't know about this option in Altair. (Sorry for the unsolicited info if it's not welcome – you obviously know what you're doing, and I'm not trying to say otherwise!) If you want to show Altair taking on the datasaurus with a drop-down list for choosing the dataset that can be standalone HTML/Javascript – not relying on a Python kernel running in the background like with ipywidgets – they've added some input widgets: Binding: Adding Data Driven Inputs

There are some things that are a bit awkward about them, but it's basically adding another selection, and then adding that to one of the plots and also filtering by it in all. Incorporating it into your example would look something like this:

dinodf = pd.read_csv('data/DatasaurusDozen.tsv',sep='\t')

input_dropdown = alt.binding_select(options=dinodf['dataset'].unique())
selection = alt.selection_single(fields=['dataset'], 
                                 bind=input_dropdown, 
                                 name='choose',
                                 init={'dataset': 'dino'})
my_si = alt.selection_interval()
axis_scale = alt.Scale(domain=[0,100])

all_base = alt.Chart(dinodf).transform_filter(selection)

points = all_base.mark_point().encode(
    x=alt.X('x', scale=axis_scale),
    y=alt.Y('y', scale=axis_scale)
).add_selection(
    selection, my_si
)

bars_base = all_base.mark_bar().transform_filter(my_si)

barsX = bars_base.encode(
    alt.X('x', bin=True, scale=axis_scale),
    y='count()'
).properties(height=100)

barsY = bars_base.encode(
    alt.Y('y', bin=True, scale=axis_scale),
    x='count()'
).properties(width=100)

chart = alt.vconcat(barsX,
            alt.hconcat(points,barsY))

chart

Since the specification was getting a little long, I did some grouping to reduce the repetition. I also fixed the scale on the scatterplot since some datasets don't go to 100. I realize both of these changes could confuse the students, so they may not be a good choice... Again, hope this is welcome – not trying to tell you how to teach your workshop!

Best, -Eric emonson@duke.edu

benjum commented 3 years ago

Hi Eric,

Thanks for reaching out! Any and all comments are most welcome. I try to absorb everything as lessons for change. :)

I really appreciate your input here for adding the drop-down menu to the altair viz. This is not something I was aware of and definitely something that I would very much like to be aware of, so I appreciate your getting in touch and letting me know about it.

I'll check this out soon and see about incorporating it into my materials. They are constantly in flux. If you have other ideas for them too, please feel free to let me know.

Thanks! Ben

On Mon, May 3, 2021 at 1:30 PM Eric E Monson @.***> wrote:

Hey Ben,

My name is Eric Monson, and I work in the Duke Libraries' Center for Data and Visualization Sciences. I really enjoyed watching your recent data visualization in Python workshop videos! I learned a few helpful things, and I'm always curious about how others teach their material.

I'm writing just in case you didn't know about this option in Altair. (Sorry for the unsolicited info if it's not welcome – you obviously know what you're doing, and I'm not trying to say otherwise!) If you want to show Altair taking on the datasaurus with a drop-down list for choosing the dataset that can be standalone HTML/Javascript – not relying on a Python kernel running in the background like with ipywidgets – they've added some input widgets: Binding: Adding Data Driven Inputs https://altair-viz.github.io/user_guide/interactions.html#binding-adding-data-driven-inputs

There are some things that are a bit awkward about them, but it's basically adding another selection, and then adding that to one of the plots and also filtering by it in all. Incorporating it into your example would look something like this:

dinodf = pd.read_csv('data/DatasaurusDozen.tsv',sep='\t')

input_dropdown = alt.binding_select(options=dinodf['dataset'].unique())

selection = alt.selection_single(fields=['dataset'],

                             bind=input_dropdown,

                             name='choose',

                             init={'dataset': 'dino'})

my_si = alt.selection_interval()

axis_scale = alt.Scale(domain=[0,100])

all_base = alt.Chart(dinodf).transform_filter(selection)

points = all_base.mark_point().encode(

x=alt.X('x', scale=axis_scale),

y=alt.Y('y', scale=axis_scale)

).add_selection(

selection, my_si

)

bars_base = all_base.mark_bar().transform_filter(my_si)

barsX = bars_base.encode(

alt.X('x', bin=True, scale=axis_scale),

y='count()'

).properties(height=100)

barsY = bars_base.encode(

alt.Y('y', bin=True, scale=axis_scale),

x='count()'

).properties(width=100)

chart = alt.vconcat(barsX,

        alt.hconcat(points,barsY))

chart

Since the specification was getting a little long, I did some grouping to reduce the repetition. I also fixed the scale on the scatterplot since some datasets don't go to 100. I realize both of these changes could confuse the students, so they may not be a good choice... Again, hope this is welcome – not trying to tell you how to teach your workshop!

Best, -Eric @.***

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/benjum/idre-spring21-python-data-viz-2/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACROJAE3Q326KT6BU5AXKD3TL4AO5ANCNFSM44BOEJ5A .