jmarsoner
jmarsoner

Reputation: 11

Dropdown filter above the Chart in altair

As default, the Dropdown filter is on the left bottom of the Chart. How do I get the Dropdown filter to be above the Chart?

Example Code:

import altair as alt
import pandas as pd

lst = [['tom', 25], ['krish', 30],
       ['nick', 26], ['juli', 22]]
    
df = pd.DataFrame(lst, columns =['Name', 'Age'])
df

name = ['tom', 'krish', 'nick', 'juli']
dropdown = alt.binding_select(options= name)
select = alt.selection_single(fields=['Name'], bind=dropdown, name = 'Selector', init={'Name': name[0]})


chart = alt.Chart(df).mark_bar().encode(
    x = alt.X('Name:N'),
    y = alt.Y('Age:Q'),
).add_selection(
    select
).transform_filter(
    select
).properties(
    width=200,
    height=250
)

chart


enter image description here

Hier, I named the Dropdown filter 'Selector_Name'. Now I want that to be not under the Chart but on the top/above the Chart. How do I do this?

Thanks.

Upvotes: 1

Views: 1181

Answers (1)

Green Finance
Green Finance

Reputation: 195

As per the answer in here (altair: change the position of a slider)

lst = [['tom', 25], ['krish', 30],
       ['nick', 26], ['juli', 22]]
    
df = pd.DataFrame(lst, columns =['Name', 'Age'])
df

name = ['tom', 'krish', 'nick', 'juli']
dropdown = alt.binding_select(options= name)
select = alt.selection_single(fields=['Name'], bind=dropdown, name = 'Selector', init={'Name': name[0]})


chart = alt.Chart(df).mark_bar().encode(
    x = alt.X('Name:N'),
    y = alt.Y('Age:Q'),
).add_selection(
    select
).transform_filter(
    select
).properties(
    width=400,
    height=450
)

display(HTML("""
<style>
form.vega-bindings {
  position: absolute;
  left: 65px;
  top: -4px;
}
</style>
"""))

chart

enter image description here

Upvotes: 2

Related Questions