From e88500e9b50c55b8e6431e3313814954e501ceef Mon Sep 17 00:00:00 2001 From: Joel Ostblom Date: Wed, 7 Dec 2022 09:08:03 -0800 Subject: [PATCH] Add another set of gallery examples --- altair/examples/interval_selection.py | 7 ++----- altair/examples/iowa_electricity.py | 20 ++++++------------- altair/examples/isotype.py | 15 ++++++++------ altair/examples/isotype_emoji.py | 11 ++++++---- altair/examples/isotype_grid.py | 6 +++--- altair/examples/lasagna_plot.py | 12 +++++------ altair/examples/layered_area_chart.py | 2 +- altair/examples/layered_bar_chart.py | 2 +- .../examples/layered_chart_with_dual_axis.py | 10 ++++------ altair/examples/layered_heatmap_text.py | 9 ++++----- altair/examples/layered_histogram.py | 4 ++-- .../examples/line_chart_with_color_datum.py | 14 +++++++------ altair/examples/line_chart_with_cumsum.py | 4 ++-- .../examples/line_chart_with_custom_legend.py | 10 +++++----- altair/examples/line_percent.py | 4 ++-- altair/examples/line_with_ci.py | 2 +- ...abeled => line_with_last_value_labeled.py} | 6 +++--- altair/examples/line_with_log_scale.py | 7 ++----- altair/examples/london_tube.py | 18 +++++------------ 19 files changed, 72 insertions(+), 91 deletions(-) rename altair/examples/{line_with_last_value_labeled => line_with_last_value_labeled.py} (86%) diff --git a/altair/examples/interval_selection.py b/altair/examples/interval_selection.py index 509c200b80..f0aa303e9c 100644 --- a/altair/examples/interval_selection.py +++ b/altair/examples/interval_selection.py @@ -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( diff --git a/altair/examples/iowa_electricity.py b/altair/examples/iowa_electricity.py index 0f2081d999..252cb04234 100644 --- a/altair/examples/iowa_electricity.py +++ b/altair/examples/iowa_electricity.py @@ -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") ) diff --git a/altair/examples/isotype.py b/altair/examples/isotype.py index 23da6f4b32..92850b729a 100644 --- a/altair/examples/isotype.py +++ b/altair/examples/isotype.py @@ -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 +) diff --git a/altair/examples/isotype_emoji.py b/altair/examples/isotype_emoji.py index 92ab0f3efc..313af8e825 100644 --- a/altair/examples/isotype_emoji.py +++ b/altair/examples/isotype_emoji.py @@ -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 +) diff --git a/altair/examples/isotype_grid.py b/altair/examples/isotype_grid.py index f90e9e8a8b..d0a9b6d4bc 100644 --- a/altair/examples/isotype_grid.py +++ b/altair/examples/isotype_grid.py @@ -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 diff --git a/altair/examples/lasagna_plot.py b/altair/examples/lasagna_plot.py index 6f603b8e18..0da7c44fd6 100644 --- a/altair/examples/lasagna_plot.py +++ b/altair/examples/lasagna_plot.py @@ -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") ) diff --git a/altair/examples/layered_area_chart.py b/altair/examples/layered_area_chart.py index fe9e42c2cf..83eb51b75a 100644 --- a/altair/examples/layered_area_chart.py +++ b/altair/examples/layered_area_chart.py @@ -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" ) diff --git a/altair/examples/layered_bar_chart.py b/altair/examples/layered_bar_chart.py index 48d221e3bb..ba485fea73 100644 --- a/altair/examples/layered_bar_chart.py +++ b/altair/examples/layered_bar_chart.py @@ -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", ) diff --git a/altair/examples/layered_chart_with_dual_axis.py b/altair/examples/layered_chart_with_dual_axis.py index 8ef8f099be..d14fbca57e 100644 --- a/altair/examples/layered_chart_with_dual_axis.py +++ b/altair/examples/layered_chart_with_dual_axis.py @@ -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' ) diff --git a/altair/examples/layered_heatmap_text.py b/altair/examples/layered_heatmap_text.py index dc912ed8b0..7a61c08cbe 100644 --- a/altair/examples/layered_heatmap_text.py +++ b/altair/examples/layered_heatmap_text.py @@ -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'), diff --git a/altair/examples/layered_histogram.py b/altair/examples/layered_histogram.py index f1af32625b..62c7dccf65 100644 --- a/altair/examples/layered_histogram.py +++ b/altair/examples/layered_histogram.py @@ -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') ) diff --git a/altair/examples/line_chart_with_color_datum.py b/altair/examples/line_chart_with_color_datum.py index 17d3a1cecf..77cad9574d 100644 --- a/altair/examples/line_chart_with_color_datum.py +++ b/altair/examples/line_chart_with_color_datum.py @@ -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 `_. """ @@ -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"] +) diff --git a/altair/examples/line_chart_with_cumsum.py b/altair/examples/line_chart_with_cumsum.py index 2b21b0845a..ef3144fe1f 100644 --- a/altair/examples/line_chart_with_cumsum.py +++ b/altair/examples/line_chart_with_cumsum.py @@ -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 @@ -21,4 +21,4 @@ x='year:O', # Plot the calculated field created by the transformation y='cumulative_wheat:Q' -).properties(width=600) \ No newline at end of file +) diff --git a/altair/examples/line_chart_with_custom_legend.py b/altair/examples/line_chart_with_custom_legend.py index 992128232a..2ee60088e3 100644 --- a/altair/examples/line_chart_with_custom_legend.py +++ b/altair/examples/line_chart_with_custom_legend.py @@ -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( @@ -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"] @@ -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 diff --git a/altair/examples/line_percent.py b/altair/examples/line_percent.py index fd23572ded..52e0470345 100644 --- a/altair/examples/line_percent.py +++ b/altair/examples/line_percent.py @@ -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' ) diff --git a/altair/examples/line_with_ci.py b/altair/examples/line_with_ci.py index 5db97915af..744f453d03 100644 --- a/altair/examples/line_with_ci.py +++ b/altair/examples/line_with_ci.py @@ -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 diff --git a/altair/examples/line_with_last_value_labeled b/altair/examples/line_with_last_value_labeled.py similarity index 86% rename from altair/examples/line_with_last_value_labeled rename to altair/examples/line_with_last_value_labeled.py index 8793cab295..be0c3f0d41 100644 --- a/altair/examples/line_with_last_value_labeled +++ b/altair/examples/line_with_last_value_labeled.py @@ -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 @@ -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' ) diff --git a/altair/examples/line_with_log_scale.py b/altair/examples/line_with_log_scale.py index b45fcd92eb..740a670cb7 100644 --- a/altair/examples/line_with_log_scale.py +++ b/altair/examples/line_with_log_scale.py @@ -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 - ) -) \ No newline at end of file + y=alt.Y('sum(people)').scale(type="log") +) diff --git a/altair/examples/london_tube.py b/altair/examples/london_tube.py index 3a39e6aef5..b19ef9accb 100644 --- a/altair/examples/london_tube.py +++ b/altair/examples/london_tube.py @@ -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( @@ -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