Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method based attribute setting to encoding classes for Altair 5 #2592

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions altair/examples/airport_connections.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Connections Among U.S. Airports Interactive
-------------------------------------------
This example shows all the connections between major U.S. airports. Lookup transformations
are used to find the coordinates of each airport and connecting airports. Connections
This example shows all the connections between major U.S. airports. Lookup transformations
are used to find the coordinates of each airport and connecting airports. Connections
are displayed on mouseover via a single selection.
"""
# category: case studies
Expand All @@ -26,10 +26,10 @@
)

background = alt.Chart(states).mark_geoshape(
fill="lightgray",
fill="lightgray",
stroke="white"
).properties(
width=750,
width=750,
height=500
).project("albersUsa")

Expand All @@ -39,11 +39,11 @@
latitude2="lat2:Q",
longitude2="lon2:Q"
).transform_lookup(
lookup="origin",
lookup="origin",
from_=lookup_data
).transform_lookup(
lookup="destination",
from_=lookup_data,
lookup="destination",
from_=lookup_data,
as_=["state", "lat2", "lon2"]
).transform_filter(
select_city
Expand All @@ -52,14 +52,14 @@
points = alt.Chart(flights_airport).mark_circle().encode(
latitude="latitude:Q",
longitude="longitude:Q",
size=alt.Size("routes:Q", scale=alt.Scale(range=[0, 1000]), legend=None),
order=alt.Order("routes:Q", sort="descending"),
size=alt.Size("routes:Q").legend(None).scale(range=[0, 1000]),
order=alt.Order("routes:Q").sort("descending"),
tooltip=["origin:N", "routes:Q"]
).transform_aggregate(
routes="count()",
routes="count()",
groupby=["origin"]
).transform_lookup(
lookup="origin",
lookup="origin",
from_=lookup_data
).transform_filter(
(alt.datum.state != "PR") & (alt.datum.state != "VI")
Expand Down
13 changes: 9 additions & 4 deletions altair/examples/annual_weather_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
source = data.seattle_weather()

alt.Chart(source, title="Daily Max Temperatures (C) in Seattle, WA").mark_rect().encode(
x=alt.X("date(date):O", title="Day", axis=alt.Axis(format="%e", labelAngle=0)),
y=alt.Y("month(date):O", title="Month"),
color=alt.Color("max(temp_max)", legend=alt.Legend(title=None)),
alt.X("date(date):O").title("Day").axis(format="%e", labelAngle=0),
alt.Y("month(date):O").title("Month"),
alt.Color("max(temp_max)").title(None),
tooltip=[
alt.Tooltip("monthdate(date)", title="Date"),
alt.Tooltip("max(temp_max)", title="Max Temp"),
],
).configure_view(step=13, strokeWidth=0).configure_axis(domain=False)
).configure_view(
step=13,
strokeWidth=0
).configure_axis(
domain=False
)
4 changes: 2 additions & 2 deletions altair/examples/anscombe_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
source = data.anscombe()

alt.Chart(source).mark_circle().encode(
alt.X('X', scale=alt.Scale(zero=False)),
alt.Y('Y', scale=alt.Scale(zero=False)),
alt.X('X').scale(zero=False),
alt.Y('Y').scale(zero=False),
alt.Facet('Series', columns=2),
).properties(
width=180,
Expand Down
12 changes: 5 additions & 7 deletions altair/examples/bar_chart_trellis_compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@
)

alt.Chart(source, width=60, height=alt.Step(8)).mark_bar().encode(
y=alt.Y("c:N", axis=None),
x=alt.X("p:Q", title=None, axis=alt.Axis(format="%")),
color=alt.Color(
"c:N", title="settings", legend=alt.Legend(orient="bottom", titleOrient="left")
),
row=alt.Row("a:N", title="Factor A", header=alt.Header(labelAngle=0)),
column=alt.Column("b:N", title="Factor B"),
alt.Y("c:N").axis(None),
alt.X("p:Q").title(None).axis(format="%"),
alt.Color("c:N").title("settings").legend(orient="bottom", titleOrient="left"),
alt.Row("a:N").title("Factor A").header(labelAngle=0),
alt.Column("b:N").title("Factor B"),
)
31 changes: 13 additions & 18 deletions altair/examples/beckers_barley_trellis_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,19 @@
source = data.barley()

alt.Chart(source, title="The Morris Mistake").mark_point().encode(
alt.X(
'yield:Q',
title="Barley Yield (bushels/acre)",
scale=alt.Scale(zero=False),
axis=alt.Axis(grid=False)
),
alt.Y(
'variety:N',
title="",
sort='-x',
axis=alt.Axis(grid=True)
),
color=alt.Color('year:N', legend=alt.Legend(title="Year")),
row=alt.Row(
'site:N',
title="",
sort=alt.EncodingSortField(field='yield', op='sum', order='descending'),
)
alt.X('yield:Q')
.title("Barley Yield (bushels/acre)")
.scale(zero=False)
.axis(grid=False),
alt.Y('variety:N')
.title("")
.sort('-x')
.axis(grid=True),
alt.Color('year:N')
.legend(title="Year"),
alt.Row('site:N')
.title("")
.sort(alt.EncodingSortField(field='yield', op='sum', order='descending'))
).properties(
height=alt.Step(20)
).configure_view(stroke="transparent")
2 changes: 1 addition & 1 deletion altair/examples/beckers_barley_wrapped_facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
source = data.barley.url

alt.Chart(source).mark_point().encode(
alt.X('median(yield):Q', scale=alt.Scale(zero=False)),
alt.X('median(yield):Q').scale(zero=False),
y='variety:O',
color='year:N',
facet=alt.Facet('site:O', columns=2),
Expand Down
6 changes: 3 additions & 3 deletions altair/examples/bump_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
stocks = data.stocks()
source = stocks.groupby([pd.Grouper(key="date", freq="6M"),"symbol"]).mean().reset_index()

alt.Chart(source).mark_line(point = True).encode(
x = alt.X("date:O", timeUnit="yearmonth", title="date"),
alt.Chart(source).mark_line(point=True).encode(
x=alt.X("date:O").timeUnit("yearmonth").title("date"),
y="rank:O",
color=alt.Color("symbol:N")
).transform_window(
Expand All @@ -26,4 +26,4 @@
title="Bump Chart for Stock Prices",
width=600,
height=150,
)
)
28 changes: 12 additions & 16 deletions altair/examples/candlestick_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@

source = data.ohlc()

open_close_color = alt.condition("datum.open <= datum.close",
alt.value("#06982d"),
alt.value("#ae1325"))
open_close_color = alt.condition(
"datum.open <= datum.close",
alt.value("#06982d"),
alt.value("#ae1325")
)

base = alt.Chart(source).encode(
alt.X('date:T',
axis=alt.Axis(
format='%m/%d',
labelAngle=-45,
title='Date in 2009'
)
),
alt.X('date:T')
.axis(format='%m/%d', labelAngle=-45)
.title('Date in 2009'),
color=open_close_color
)

rule = base.mark_rule().encode(
alt.Y(
'low:Q',
title='Price',
scale=alt.Scale(zero=False),
),
alt.Y('low:Q')
.title('Price')
.scale(zero=False),
alt.Y2('high:Q')
)

Expand All @@ -41,4 +37,4 @@
alt.Y2('close:Q')
)

rule + bar
rule + bar
29 changes: 14 additions & 15 deletions altair/examples/co2_concentration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@
groupby=['decade'],
frame=[None, None]
).transform_calculate(
end="datum.first_date === datum.scaled_date ? 'first' : datum.last_date === datum.scaled_date ? 'last' : null"
end=(
"datum.first_date === datum.scaled_date ? 'first'"
": datum.last_date === datum.scaled_date ? 'last'"
": null"
)
).encode(
x=alt.X(
"scaled_date:Q",
axis=alt.Axis(title="Year into Decade", tickCount=11)
),
y=alt.Y(
"CO2:Q",
title="CO2 concentration in ppm",
scale=alt.Scale(zero=False)
)
alt.X("scaled_date:Q")
.title("Year into Decade")
.axis(tickCount=11),
alt.Y("CO2:Q")
.title("CO2 concentration in ppm")
.scale(zero=False)
)

line = base.mark_line().encode(
color=alt.Color(
"decade:O",
scale=alt.Scale(scheme="magma"),
legend=None
)
alt.Color("decade:O")
.scale(scheme="magma")
.legend(None)
)

text = base.encode(text="year:N")
Expand Down
48 changes: 30 additions & 18 deletions altair/examples/comet_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@
import altair as alt
import vega_datasets

(
alt.Chart(vega_datasets.data.barley.url)
.transform_pivot("year", value="yield", groupby=["variety", "site"])
.transform_fold(["1931", "1932"], as_=["year", "yield"])
.transform_calculate(calculate="datum['1932'] - datum['1931']", as_="delta")
.mark_trail()
.encode(
x=alt.X('year:O', title=None),
y=alt.Y('variety:N', title='Variety'),
size=alt.Size('yield:Q', scale=alt.Scale(range=[0, 12]), legend=alt.Legend(values=[20, 60], title='Barley Yield (bushels/acre)')),
color=alt.Color('delta:Q', scale=alt.Scale(domainMid=0), legend=alt.Legend(title='Yield Delta (%)')),
tooltip=alt.Tooltip(['year:O', 'yield:Q']),
column=alt.Column('site:N', title='Site')

)
.configure_view(stroke=None)
.configure_legend(orient='bottom', direction='horizontal')
.properties(title='Barley Yield comparison between 1932 and 1931')
alt.Chart(
vega_datasets.data.barley.url,
title='Barley Yield comparison between 1932 and 1931'
).mark_trail().encode(
alt.X('year:O').title(None),
alt.Y('variety:N').title('Variety'),
alt.Size('yield:Q')
.scale(range=[0, 12])
.legend(values=[20, 60])
.title('Barley Yield (bushels/acre)'),
alt.Color('delta:Q')
.scale(domainMid=0)
.title('Yield Delta (%)'),
alt.Tooltip(['year:O', 'yield:Q']),
alt.Column('site:N').title('Site')
).transform_pivot(
"year",
value="yield",
groupby=["variety", "site"]
).transform_fold(
["1931", "1932"],
as_=["year", "yield"]
).transform_calculate(
calculate="datum['1932'] - datum['1931']",
as_="delta"
).configure_legend(
orient='bottom',
direction='horizontal'
).configure_view(
stroke=None
)
4 changes: 2 additions & 2 deletions altair/examples/connected_scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
source = data.driving()

alt.Chart(source).mark_line(point=True).encode(
alt.X('miles', scale=alt.Scale(zero=False)),
alt.Y('gas', scale=alt.Scale(zero=False)),
alt.X('miles').scale(zero=False),
alt.Y('gas').scale(zero=False),
order='year'
)
2 changes: 1 addition & 1 deletion altair/examples/density_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
steps=200
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q', stack='zero'),
alt.Y('density:Q').stack('zero'),
alt.Color('Measurement_type:N')
).properties(width=400, height=100)
8 changes: 2 additions & 6 deletions altair/examples/diverging_stacked_bar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@
alt.Chart(source).mark_bar().encode(
x='percentage_start:Q',
x2='percentage_end:Q',
y=alt.Y('question:N', axis=y_axis),
color=alt.Color(
'type:N',
legend=alt.Legend( title='Response'),
scale=color_scale,
)
y=alt.Y('question:N').axis(y_axis),
color=alt.Color('type:N').title('Response').scale(color_scale),
)
9 changes: 6 additions & 3 deletions altair/examples/donut_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import pandas as pd
import altair as alt

source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})
source = pd.DataFrame({
"category": [1, 2, 3, 4, 5, 6],
"value": [4, 6, 10, 3, 7, 8]
})

alt.Chart(source).mark_arc(innerRadius=50).encode(
theta=alt.Theta(field="value", type="quantitative"),
color=alt.Color(field="category", type="nominal"),
theta="value",
color="category:N",
)
12 changes: 6 additions & 6 deletions altair/examples/dot_dash_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

# Configure the points
points = base.mark_point().encode(
x=alt.X('Miles_per_Gallon', title=''),
y=alt.Y('Horsepower', title=''),
alt.X('Miles_per_Gallon').title(''),
alt.Y('Horsepower').title(''),
color=alt.condition(brush, 'Origin', alt.value('grey'))
)

# Configure the ticks
tick_axis = alt.Axis(labels=False, domain=False, ticks=False)

x_ticks = base.mark_tick().encode(
alt.X('Miles_per_Gallon', axis=tick_axis),
alt.Y('Origin', title='', axis=tick_axis),
alt.X('Miles_per_Gallon').axis(tick_axis),
alt.Y('Origin').title('').axis(tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)

y_ticks = base.mark_tick().encode(
alt.X('Origin', title='', axis=tick_axis),
alt.Y('Horsepower', axis=tick_axis),
alt.X('Origin').title('').axis(tick_axis),
alt.Y('Horsepower').axis(tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)

Expand Down
Loading