In [2]:
import altair as alt
In [17]:
from vega_datasets import data
c1=data.cars()
sel1=alt.selection_interval()
h1=alt.Chart(c1).mark_point().encode(x='Weight_in_lbs',y='Displacement',color="Origin")#.add_selection(sel1)
h1.interactive()
Out[17]:
In [13]:
h1.selection
Out[13]:
{'selector003': SelectionDef({
   type: 'interval'
 })}
In [14]:
import altair as alt
from vega_datasets import data

source = data.seattle_weather()
brush = alt.selection(type='interval', encodings=['x'])

bars = alt.Chart().mark_bar().encode(
    x='month(date):O',
    y='mean(precipitation):Q',
    opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7)),
).add_selection(
    brush
)

line = alt.Chart().mark_rule(color='firebrick').encode(
    y='mean(precipitation):Q',
    size=alt.SizeValue(3)
).transform_filter(
    brush
)

alt.layer(bars, line, data=source)
Out[14]:
In [15]:
import pandas as pd
import numpy as np

np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
                    columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')

# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
                        fields=['x'], empty='none')

# The basic line
line = alt.Chart(source).mark_line(interpolate='basis').encode(
    x='x:Q',
    y='y:Q',
    color='category:N'
)

# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart(source).mark_point().encode(
    x='x:Q',
    opacity=alt.value(0),
).add_selection(
    nearest
)

# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
)

# Draw text labels near the points, and highlight based on selection
text = line.mark_text(align='left', dx=5, dy=-5).encode(
    text=alt.condition(nearest, 'y:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
rules = alt.Chart(source).mark_rule(color='gray').encode(
    x='x:Q',
).transform_filter(
    nearest
)

# Put the five layers into a chart and bind the data
alt.layer(
    line, selectors, points, rules, text
).properties(
    width=600, height=300
)
Out[15]:
In [21]:
import altair as alt
from vega_datasets import data

source = data.cars()

brush = alt.selection(type='interval')

points = alt.Chart(source).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
    brush
)

bars = alt.Chart(source).mark_bar().encode(
    x='Origin:N',
    color='Origin:N',
    y='count(Origin):Q'
).transform_filter(
    brush
)

points | bars
Out[21]:
In [81]:
source = alt.topo_feature(data.world_110m.url, 'countries')

base = alt.Chart(source).mark_geoshape(fill='#999999', stroke='white')#.interactive()
proj='orthographic'
proj='naturalEarth1'
grat=alt.Chart(alt.graticule()).mark_geoshape(stroke='black', strokeWidth=0.5)
path=pd.DataFrame({'x':np.r_[-10,0,10,40],'y':np.r_[-30,0,35,50],'rate':[1,4,5,7]})
myx=alt.X('x', bin = True,scale=alt.Scale(domain=[-180, 180]))
myy=alt.X('y', bin = True,scale=alt.Scale(domain=[-90, 90]))
myrate=alt.X('rate:Q', bin = True,scale=alt.Scale(domain=[0,10]))
line = alt.Chart(path).mark_line(interpolate='basis',color='red',point=True).encode(x=myx,y=myy,tooltip='rate:N')#,size=myrate)
exarg={'translate':[140,140],'scale':130}
out=alt.layer(base,grat,line).project(proj).properties(title='orthographic').properties(width=700,height=380)
out# + grat
Out[81]:
In [33]:
out.save("world.html")
In [ ]: