Skip to content

Commit

Permalink
Add another set of gallery examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joelostblom committed Dec 7, 2022
1 parent 734f003 commit e88500e
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 91 deletions.
7 changes: 2 additions & 5 deletions altair/examples/interval_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@

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

base = alt.Chart(source).mark_area().encode(
base = alt.Chart(source, width=600, height=200).mark_area().encode(
x = 'date:T',
y = 'price:Q'
).properties(
width=600,
height=200
)

upper = base.encode(
alt.X('date:T', scale=alt.Scale(domain=brush))
alt.X('date:T').scale(domain=brush)
)

lower = base.properties(
Expand Down
20 changes: 6 additions & 14 deletions altair/examples/iowa_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@
source = data.iowa_electricity()

alt.Chart(source, title="Iowa's renewable energy boom").mark_area().encode(
x=alt.X(
"year:T",
title="Year"
),
y=alt.Y(
"net_generation:Q",
stack="normalize",
title="Share of net generation",
axis=alt.Axis(format=".0%"),
),
color=alt.Color(
"source:N",
legend=alt.Legend(title="Electricity source"),
)
alt.X("year:T").title("Year"),
alt.Y("net_generation:Q")
.title("Share of net generation")
.stack("normalize")
.axis(format=".0%"),
alt.Color("source:N").title("Electricity source")
)
15 changes: 9 additions & 6 deletions altair/examples/isotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@
)

alt.Chart(source).mark_point(filled=True, opacity=1, size=100).encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.Shape('animal:N', legend=None, scale=shape_scale),
alt.Color('animal:N', legend=None, scale=color_scale),
alt.X('x:O').axis(None),
alt.Y('animal:O').axis(None),
alt.Row('country:N').header(title=''),
alt.Shape('animal:N').legend(None).scale(shape_scale),
alt.Color('animal:N').legend(None).scale(color_scale),
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)
).properties(
width=550,
height=140
)
11 changes: 7 additions & 4 deletions altair/examples/isotype_emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@


alt.Chart(source).mark_text(size=45, baseline='middle').encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.X('x:O').axis(None),
alt.Y('animal:O').axis(None),
alt.Row('country:N').title(''),
alt.Text('emoji:N')
).transform_calculate(
emoji="{'cattle': '🐄', 'pigs': '🐖', 'sheep': '🐏'}[datum.animal]"
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)
).properties(
width=550,
height=140
)
6 changes: 3 additions & 3 deletions altair/examples/isotype_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
filled=True,
size=50
).encode(
x=alt.X("col:O", axis=None),
y=alt.Y("row:O", axis=None),
shape=alt.ShapeValue(person)
alt.X("col:O").axis(None),
alt.Y("row:O").axis(None),
alt.ShapeValue(person)
).properties(
width=400,
height=400
Expand Down
12 changes: 5 additions & 7 deletions altair/examples/lasagna_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
alt.Chart(source, width=300, height=100).transform_filter(
alt.datum.symbol != "GOOG"
).mark_rect().encode(
x=alt.X(
"yearmonthdate(date):O",
axis=alt.Axis(
alt.X("yearmonthdate(date):O")
.title("Time")
.axis(
format="%Y",
labelAngle=0,
labelOverlap=False,
labelColor=color_condition,
tickColor=color_condition,
),
title="Time",
),
y=alt.Y("symbol:N", title=None),
color=alt.Color("sum(price)", title="Price"),
alt.Y("symbol:N").title(None),
alt.Color("sum(price)").title("Price")
)
2 changes: 1 addition & 1 deletion altair/examples/layered_area_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

alt.Chart(source).mark_area(opacity=0.3).encode(
x="year:T",
y=alt.Y("net_generation:Q", stack=None),
y=alt.Y("net_generation:Q").stack(None),
color="source:N"
)
2 changes: 1 addition & 1 deletion altair/examples/layered_bar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

alt.Chart(source).mark_bar(opacity=0.7).encode(
x='year:O',
y=alt.Y('net_generation:Q', stack=None),
y=alt.Y('net_generation:Q').stack(None),
color="source",
)
10 changes: 4 additions & 6 deletions altair/examples/layered_chart_with_dual_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
source = data.seattle_weather()

base = alt.Chart(source).encode(
alt.X('month(date):T', axis=alt.Axis(title=None))
alt.X('month(date):T').axis(title=None)
)

area = base.mark_area(opacity=0.3, color='#57A44C').encode(
alt.Y('average(temp_max)',
axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
alt.Y('average(temp_max)').title('Avg. Temperature (°C)', titleColor='#57A44C'),
alt.Y2('average(temp_min)')
)

line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
alt.Y('average(precipitation)',
axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7'))
alt.Y('average(precipitation)').title('Precipitation (inches)', titleColor='#5276A7')
)

alt.layer(area, line).resolve_scale(
y = 'independent'
y='independent'
)
9 changes: 4 additions & 5 deletions altair/examples/layered_heatmap_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@

# Configure heatmap
heatmap = base.mark_rect().encode(
color=alt.Color('mean_horsepower:Q',
scale=alt.Scale(scheme='viridis'),
legend=alt.Legend(title="Mean of Horsepower"),
)
alt.Color('mean_horsepower:Q')
.scale(scheme='viridis')
.title("Mean of Horsepower")
)

# Configure text
text = base.mark_text(baseline='middle').encode(
text=alt.Text('mean_horsepower:Q', format=".0f"),
alt.Text('mean_horsepower:Q', format=".0f"),
color=alt.condition(
alt.datum.mean_horsepower > 150,
alt.value('black'),
Expand Down
4 changes: 2 additions & 2 deletions altair/examples/layered_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
opacity=0.3,
binSpacing=0
).encode(
alt.X('Measurement:Q', bin=alt.Bin(maxbins=100)),
alt.Y('count()', stack=None),
alt.X('Measurement:Q').bin(maxbins=100),
alt.Y('count()').stack(None),
alt.Color('Experiment:N')
)
14 changes: 8 additions & 6 deletions altair/examples/line_chart_with_color_datum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Line Chart with Datum for Color
-------------------------------
An example of using ``datum`` and ``repeat`` to color a multi-series line chart.
An example of using ``repeat`` inside ``datum`` to color a multi-series line chart.
This is adapted from this corresponding Vega-Lite Example:
`Repeat and Layer to Show Different Movie Measures <https://vega.github.io/vega-lite/examples/repeat_layer.html>`_.
"""
Expand All @@ -13,9 +13,11 @@
source = data.movies()

alt.Chart(source).mark_line().encode(
x=alt.X("IMDB_Rating", bin=True),
y=alt.Y(
alt.repeat("layer"), aggregate="mean", title="Mean of US and Worldwide Gross"
),
alt.X("IMDB_Rating").bin(True),
alt.Y(alt.repeat("layer"))
.aggregate("mean")
.title("Mean of US and Worldwide Gross"),
color=alt.datum(alt.repeat("layer")),
).repeat(layer=["US_Gross", "Worldwide_Gross"])
).repeat(
layer=["US_Gross", "Worldwide_Gross"]
)
4 changes: 2 additions & 2 deletions altair/examples/line_chart_with_cumsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

source = data.wheat()

alt.Chart(source).mark_line().transform_window(
alt.Chart(source, width=600).mark_line().transform_window(
# Sort the data chronologically
sort=[{'field': 'year'}],
# Include all previous records before the current record and none after
Expand All @@ -21,4 +21,4 @@
x='year:O',
# Plot the calculated field created by the transformation
y='cumulative_wheat:Q'
).properties(width=600)
)
10 changes: 5 additions & 5 deletions altair/examples/line_chart_with_custom_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
source = data.stocks()

base = alt.Chart(source).encode(
color=alt.Color("symbol", legend=None)
alt.Color("symbol").legend(None)
).transform_filter(
"datum.symbol !== 'IBM'"
).properties(
Expand All @@ -23,8 +23,8 @@


last_price = base.mark_circle().encode(
x=alt.X("last_date['date']:T"),
y=alt.Y("last_date['price']:Q")
alt.X("last_date['date']:T"),
alt.Y("last_date['price']:Q")
).transform_aggregate(
last_date="argmax(date)",
groupby=["symbol"]
Expand All @@ -33,8 +33,8 @@
company_name = last_price.mark_text(align="left", dx=4).encode(text="symbol")

chart = (line + last_price + company_name).encode(
x=alt.X(title="date"),
y=alt.Y(title="price")
x=alt.X().title("date"),
y=alt.Y().title("price")
)

chart
4 changes: 2 additions & 2 deletions altair/examples/line_percent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

alt.Chart(source).mark_line().encode(
alt.X('year:O'),
alt.Y('perc:Q', axis=alt.Axis(format='%')),
color='sex:N'
alt.Y('perc:Q').axis(format='%'),
alt.Color('sex:N')
).transform_filter(
alt.datum.job == 'Welder'
)
2 changes: 1 addition & 1 deletion altair/examples/line_with_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

band = alt.Chart(source).mark_errorband(extent='ci').encode(
x='Year',
y=alt.Y('Miles_per_Gallon', title='Miles/Gallon'),
y=alt.Y('Miles_per_Gallon').title('Miles/Gallon'),
)

band + line
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
chart = alt.Chart(source).transform_filter(
alt.datum.symbol != "IBM" # A reducation of the dataset to clarify our example. Not required.
).encode(
color=alt.Color("symbol", legend=None)
alt.Color("symbol").legend(None)
)

# Draw the line
Expand All @@ -25,8 +25,8 @@

# Use the `argmax` aggregate to limit the dataset to the final value
label = chart.encode(
x=alt.X('max(date):T'),
y=alt.Y('price:Q', aggregate=alt.ArgmaxDef(argmax='date')),
x='max(date):T',
y=alt.Y('price:Q').aggregate(alt.ArgmaxDef(argmax='date')),
text='symbol'
)

Expand Down
7 changes: 2 additions & 5 deletions altair/examples/line_with_log_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@

alt.Chart(source).mark_line().encode(
x='year:O',
y=alt.Y(
'sum(people)',
scale=alt.Scale(type="log") # Here the scale is applied
)
)
y=alt.Y('sum(people)').scale(type="log")
)
18 changes: 5 additions & 13 deletions altair/examples/london_tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
tubelines = alt.topo_feature(data.londonTubeLines.url, 'line')
centroids = data.londonCentroids.url

background = alt.Chart(boroughs).mark_geoshape(
background = alt.Chart(boroughs, width=700, height=500).mark_geoshape(
stroke='white',
strokeWidth=2
).encode(
color=alt.value('#eee'),
).properties(
width=700,
height=500
)

labels = alt.Chart(centroids).mark_text().encode(
Expand All @@ -45,15 +42,10 @@
filled=False,
strokeWidth=2
).encode(
alt.Color(
'id:N',
legend=alt.Legend(
title=None,
orient='bottom-right',
offset=0
),
scale=line_scale
)
alt.Color('id:N')
.title(None)
.legend(orient='bottom-right', offset=0)
.scale(line_scale)
)

background + labels + lines

0 comments on commit e88500e

Please sign in to comment.