This notebook contains a number of examples of scatter-plot charts.
import altair as alt
from vega_datasets import data
cars = data.cars()
cars.head()
Name | Miles_per_Gallon | Cylinders | Displacement | Horsepower | Weight_in_lbs | Acceleration | Year | Origin | |
---|---|---|---|---|---|---|---|---|---|
0 | chevrolet chevelle malibu | 18.0 | 8 | 307.0 | 130.0 | 3504 | 12.0 | 1970-01-01 | USA |
1 | buick skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693 | 11.5 | 1970-01-01 | USA |
2 | plymouth satellite | 18.0 | 8 | 318.0 | 150.0 | 3436 | 11.0 | 1970-01-01 | USA |
3 | amc rebel sst | 16.0 | 8 | 304.0 | 150.0 | 3433 | 12.0 | 1970-01-01 | USA |
4 | ford torino | 17.0 | 8 | 302.0 | 140.0 | 3449 | 10.5 | 1970-01-01 | USA |
alt.Chart(cars).mark_point().encode(
x='Horsepower',
)
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon'
).interactive()
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
size='Acceleration'
)
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
shape='Origin',
)
alt.Chart(cars).mark_circle().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
)
alt.Chart(cars).mark_square().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
)
alt.Chart(cars).mark_text().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
text='Origin'
)
alt.Chart(cars).mark_tick().encode(
x='Horsepower:Q',
y='Cylinders:O',
)
chart = alt.Chart(cars).mark_point().encode(
y='Horsepower:Q',
color='Origin:N'
).interactive()
chart.encode(x='Acceleration:Q') | chart.encode(x='Displacement:Q')
alt.Chart(cars).mark_point().encode(
alt.X(alt.repeat('column'), type='quantitative'),
alt.Y(alt.repeat('row'), type='quantitative'),
color='Origin:N'
).properties(
width=250,
height=250
).repeat(
row=['Horsepower', 'Miles_per_Gallon'],
column=['Acceleration', 'Displacement']
).interactive()
brush = alt.selection_interval()
alt.Chart(cars).mark_point().encode(
alt.X(alt.repeat('column'), type='quantitative'),
alt.Y(alt.repeat('row'), type='quantitative'),
color=alt.condition(brush, 'Origin:N', alt.value('gray'))
).add_selection(
brush
).properties(
width=250,
height=250,
).repeat(
row=['Horsepower', 'Miles_per_Gallon'],
column=['Acceleration', 'Displacement']
)