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

WIP: Tab completion 2 #2770

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7980164
Add method based syntax from #1629 to v5
joelostblom Apr 16, 2022
91ddea9
Add correct function signatures and docstrings, and initial autocompl…
joelostblom May 13, 2022
9fa0c38
Merge branch 'altair-viz:master' into method-based-attr-setting
joelostblom Nov 24, 2022
cc5ca7c
Add general param info to docstrings and pass through adding the help…
joelostblom Nov 30, 2022
de8c650
Deal with docstring missing "Attributes" tag
joelostblom Nov 30, 2022
3e45b79
Fix black formatting
joelostblom Nov 30, 2022
0bc0aa1
Fix black formatting
joelostblom Nov 30, 2022
d749916
Update a few gallery examples to the method based syntax
joelostblom Dec 5, 2022
c33b093
Add test for method based attr setting
joelostblom Dec 5, 2022
34d83f5
Convert timeunit param to method
joelostblom Dec 5, 2022
adddf46
Convert a few additional gallery examples
joelostblom Dec 5, 2022
7db01fc
Convert a few additional gallery examples
joelostblom Dec 5, 2022
8992c96
Update relative imports syntax
joelostblom Dec 5, 2022
93640d4
Remove unused import
joelostblom Dec 5, 2022
734f003
Only generate new schema for the latest version of vega lite to avoid…
joelostblom Dec 5, 2022
f323915
Add another set of gallery examples
joelostblom Dec 7, 2022
52e6ada
Add another set of gallery examples
joelostblom Dec 8, 2022
ffcca0c
Add last set of gallery examples
joelostblom Dec 15, 2022
557d0c8
Type hints for tab completion
ChristopherDavisUCI Dec 23, 2022
c24aa4a
Fix NoneType notation
ChristopherDavisUCI Dec 23, 2022
394e3c8
Replace Type[None] with None
ChristopherDavisUCI Dec 27, 2022
890637b
Move examples with the new attribute syntax
ChristopherDavisUCI Dec 30, 2022
ce4cace
Merge remote-tracking branch 'upstream/master' into tab-complete2
ChristopherDavisUCI Dec 30, 2022
e7ae0ba
Basic documentation for attribute setter methods
ChristopherDavisUCI Dec 30, 2022
29169bc
Apply suggestions from code review
ChristopherDavisUCI Dec 30, 2022
c30ba34
Merge branch 'master' into tab-complete2
mattijn Dec 31, 2022
d7d4806
include attribute_syntax charts in tests
mattijn Dec 31, 2022
f30d037
add pacman chart with datum variant
mattijn Dec 31, 2022
32b0390
chart had extension missing
mattijn Dec 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 70 additions & 0 deletions altair/examples/attribute_syntax/airport_connections.py
@@ -0,0 +1,70 @@
"""
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
are displayed on mouseover via a single selection.
"""
# category: case studies
import altair as alt
from vega_datasets import data

# Since these data are each more than 5,000 rows we'll import from the URLs
airports = data.airports.url
flights_airport = data.flights_airport.url

states = alt.topo_feature(data.us_10m.url, feature="states")

# Create mouseover selection
select_city = alt.selection_point(
on="mouseover", nearest=True, fields=["origin"], empty=False
)

# Define which attributes to lookup from airports.csv
lookup_data = alt.LookupData(
airports, key="iata", fields=["state", "latitude", "longitude"]
)

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

connections = alt.Chart(flights_airport).mark_rule(opacity=0.35).encode(
latitude="latitude:Q",
longitude="longitude:Q",
latitude2="lat2:Q",
longitude2="lon2:Q"
).transform_lookup(
lookup="origin",
from_=lookup_data
).transform_lookup(
lookup="destination",
from_=lookup_data,
as_=["state", "lat2", "lon2"]
).transform_filter(
select_city
)

points = alt.Chart(flights_airport).mark_circle().encode(
latitude="latitude:Q",
longitude="longitude:Q",
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()",
groupby=["origin"]
).transform_lookup(
lookup="origin",
from_=lookup_data
).transform_filter(
(alt.datum.state != "PR") & (alt.datum.state != "VI")
).add_params(
select_city
)

(background + connections + points).configure_view(stroke=None)
24 changes: 24 additions & 0 deletions altair/examples/attribute_syntax/annual_weather_heatmap.py
@@ -0,0 +1,24 @@
"""
Annual Weather Heatmap
----------------------
"""
# category: tables
import altair as alt
from vega_datasets import data

source = data.seattle_weather()

alt.Chart(source, title="Daily Max Temperatures (C) in Seattle, WA").mark_rect().encode(
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
)
20 changes: 20 additions & 0 deletions altair/examples/attribute_syntax/anscombe_plot.py
@@ -0,0 +1,20 @@
"""
Anscombe's Quartet
------------------

This example shows how to use the column channel to make a trellis plot. Anscombe's Quartet is a famous dataset constructed by Francis Anscombe. Common summary statistics are identical for each subset of the data, despite the subsets having vastly different characteristics.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.anscombe()

alt.Chart(source).mark_circle().encode(
alt.X('X').scale(zero=False),
alt.Y('Y').scale(zero=False),
alt.Facet('Series', columns=2),
).properties(
width=180,
height=180,
)
48 changes: 48 additions & 0 deletions altair/examples/attribute_syntax/bar_chart_trellis_compact.py
@@ -0,0 +1,48 @@
"""
Compact Trellis Grid of Bar Charts
==================================
This example shows a simple grid of bar charts to compare performance data..
"""
# category: bar charts
import altair as alt
import pandas as pd

source = pd.DataFrame(
[
{"a": "a1", "b": "b1", "c": "x", "p": "0.14"},
{"a": "a1", "b": "b1", "c": "y", "p": "0.60"},
{"a": "a1", "b": "b1", "c": "z", "p": "0.03"},
{"a": "a1", "b": "b2", "c": "x", "p": "0.80"},
{"a": "a1", "b": "b2", "c": "y", "p": "0.38"},
{"a": "a1", "b": "b2", "c": "z", "p": "0.55"},
{"a": "a1", "b": "b3", "c": "x", "p": "0.11"},
{"a": "a1", "b": "b3", "c": "y", "p": "0.58"},
{"a": "a1", "b": "b3", "c": "z", "p": "0.79"},
{"a": "a2", "b": "b1", "c": "x", "p": "0.83"},
{"a": "a2", "b": "b1", "c": "y", "p": "0.87"},
{"a": "a2", "b": "b1", "c": "z", "p": "0.67"},
{"a": "a2", "b": "b2", "c": "x", "p": "0.97"},
{"a": "a2", "b": "b2", "c": "y", "p": "0.84"},
{"a": "a2", "b": "b2", "c": "z", "p": "0.90"},
{"a": "a2", "b": "b3", "c": "x", "p": "0.74"},
{"a": "a2", "b": "b3", "c": "y", "p": "0.64"},
{"a": "a2", "b": "b3", "c": "z", "p": "0.19"},
{"a": "a3", "b": "b1", "c": "x", "p": "0.57"},
{"a": "a3", "b": "b1", "c": "y", "p": "0.35"},
{"a": "a3", "b": "b1", "c": "z", "p": "0.49"},
{"a": "a3", "b": "b2", "c": "x", "p": "0.91"},
{"a": "a3", "b": "b2", "c": "y", "p": "0.38"},
{"a": "a3", "b": "b2", "c": "z", "p": "0.91"},
{"a": "a3", "b": "b3", "c": "x", "p": "0.99"},
{"a": "a3", "b": "b3", "c": "y", "p": "0.80"},
{"a": "a3", "b": "b3", "c": "z", "p": "0.37"},
]
)

alt.Chart(source, width=60, height=alt.Step(8)).mark_bar().encode(
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"),
)
28 changes: 28 additions & 0 deletions altair/examples/attribute_syntax/beckers_barley_trellis_plot.py
@@ -0,0 +1,28 @@
"""
Becker's Barley Trellis Plot
----------------------------
The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s. Using the visualization technique below they identified an anomoly in a widely used agriculatural dataset, which they termed `"The Morris Mistake." <http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf>`_. It became their favored way of showcasing the power of this pioneering plot.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source, title="The Morris Mistake").mark_point().encode(
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")
22 changes: 22 additions & 0 deletions altair/examples/attribute_syntax/beckers_barley_wrapped_facet.py
@@ -0,0 +1,22 @@
"""
Becker's Barley Trellis Plot (Wrapped Facet)
--------------------------------------------
The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s.
This is the Altair replicate of `the VegaLite version <https://vega.github.io/vega-lite/docs/facet.html#facet-full>`_
demonstrating the usage of `columns` argument to create wrapped facet.
"""
# category: advanced calculations
import altair as alt
from vega_datasets import data

source = data.barley.url

alt.Chart(source).mark_point().encode(
alt.X('median(yield):Q').scale(zero=False),
y='variety:O',
color='year:N',
facet=alt.Facet('site:O', columns=2),
).properties(
width=200,
height=100,
)
29 changes: 29 additions & 0 deletions altair/examples/attribute_syntax/bump_chart.py
@@ -0,0 +1,29 @@
"""
Bump Chart
----------
This example shows a bump chart. The data is first grouped into six-month
intervals using pandas. The ranks are computed by Altair using a
window transform.
"""
# category: line charts

import altair as alt
from vega_datasets import data
import pandas as pd

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"),
y="rank:O",
color=alt.Color("symbol:N")
).transform_window(
rank="rank()",
sort=[alt.SortField("price", order="descending")],
groupby=["date"]
).properties(
title="Bump Chart for Stock Prices",
width=600,
height=150,
)
40 changes: 40 additions & 0 deletions altair/examples/attribute_syntax/candlestick_chart.py
@@ -0,0 +1,40 @@
"""
Candlestick Chart
=================
A candlestick chart inspired from `Protovis <http://mbostock.github.io/protovis/ex/candlestick.html>`_.
This example shows the performance of the Chicago Board Options Exchange `Volatility Index <https://en.wikipedia.org/wiki/VIX>`_ (VIX)
in the summer of 2009. The thick bar represents the opening and closing prices,
while the thin bar shows intraday high and low prices; if the index closed higher on a given day, the bars are colored green rather than red.
"""
# category: advanced calculations
import altair as alt
from vega_datasets import data

source = data.ohlc()

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(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(zero=False),
alt.Y2('high:Q')
)

bar = base.mark_bar().encode(
alt.Y('open:Q'),
alt.Y2('close:Q')
)

rule + bar
64 changes: 64 additions & 0 deletions altair/examples/attribute_syntax/co2_concentration.py
@@ -0,0 +1,64 @@
"""
Atmospheric CO2 Concentration
-----------------------------
This example is a fully developed line chart that uses a window transformation.
It was inspired by `Gregor Aisch's work at datawrapper
<https://www.datawrapper.de/_/OHgEm/>`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.co2_concentration.url

base = alt.Chart(
source,
title="Carbon Dioxide in the Atmosphere"
).transform_calculate(
year="year(datum.Date)"
).transform_calculate(
decade="floor(datum.year / 10)"
).transform_calculate(
scaled_date="(datum.year % 10) + (month(datum.Date)/12)"
).transform_window(
first_date='first_value(scaled_date)',
last_date='last_value(scaled_date)',
sort=[{"field": "scaled_date", "order": "ascending"}],
groupby=['decade'],
frame=[None, None]
).transform_calculate(
end=(
"datum.first_date === datum.scaled_date ? 'first'"
": datum.last_date === datum.scaled_date ? 'last'"
": null"
)
).encode(
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(
alt.Color("decade:O")
.scale(scheme="magma")
.legend(None)
)

text = base.encode(text="year:N")

start_year = text.transform_filter(
alt.datum.end == 'first'
).mark_text(baseline="top")

end_year = text.transform_filter(
alt.datum.end == 'last'
).mark_text(baseline="bottom")

(line + start_year + end_year).configure_text(
align="left",
dx=1,
dy=3
).properties(width=600, height=375)
44 changes: 44 additions & 0 deletions altair/examples/attribute_syntax/comet_chart.py
@@ -0,0 +1,44 @@
"""
Comet Chart
-----------
Inspired by `Zan Armstrong's comet chart <https://www.zanarmstrong.com/infovisresearch>`_
this plot uses ``mark_trail`` to visualize change of grouped data over time.
A more elaborate example and explanation of creating comet charts in Altair
is shown in `this blogpost <https://medium.com/de-dataverbinders/comet-charts-in-python-visualizing-statistical-mix-effects-and-simpsons-paradox-with-altair-6cd51fb58b7c>`_.
"""
# category: advanced calculations

import altair as alt
import vega_datasets

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
)