Skip to content

Commit

Permalink
Support choropleths for Matplotlib backend via Altair (#322)
Browse files Browse the repository at this point in the history
* Use altair/Choropleth.py in MatpltlibRenderer.py to render choropleths; include warning

* Remove newline

* Move import; uncomment return statement

* minor edit to warning text

* Add message to info rather than Python warning

* Move message into info tab; correct warning message based on Doris's commit in caa9d15; condition on matplotlib

* Add corresponding matplotlib test in test_vis.py

Co-authored-by: Doris Lee <dorisjunglinlee@gmail.com>
  • Loading branch information
micahtyong and dorisjlee committed Apr 10, 2021
1 parent 064ca68 commit 952d3c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lux/action/univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ def univariate(ldf, *args):
examples = f" (e.g., {possible_attributes[0]})"
intent = [lux.Clause("?", data_type="geographical"), lux.Clause("?", data_model="measure")]
intent.extend(filter_specs)
long_description = f"Geographical displays <a href='https://en.wikipedia.org/wiki/Choropleth_map'>choropleths</a> for geographic attribute{examples}, with colors indicating the average measure values. "
if lux.config.plotting_backend == "matplotlib":
long_description += "The map visualizations from the 'Geographical' tab are rendered using <a href='https://altair-viz.github.io/'>Altair</a>. Lux does not currently support geographical maps with Matplotlib. If you would like this feature, please leave us a comment at <a href='https://github.com/lux-org/lux/issues/310'>issue #310</a> to let us know!"
recommendation = {
"action": "Geographical",
"description": "Show choropleth maps of <p class='highlight-descriptor'>geographic</p> attributes",
"long_description": f"Occurence displays choropleths of averages for some geographic attribute{examples}. Visualizations are ranked by diversity of the geographic attribute.",
"long_description": long_description,
}
elif data_type_constraint == "temporal":
intent = [lux.Clause("?", data_type="temporal")]
Expand Down
2 changes: 1 addition & 1 deletion lux/vislib/altair/Choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, dobj):
super().__init__(dobj)

def __repr__(self):
return f"Proportional Symbol Map <{str(self.vis)}>"
return f"Choropleth Map <{str(self.vis)}>"

def initialize_chart(self):
x_attr = self.vis.get_attr_by_channel("x")[0]
Expand Down
3 changes: 3 additions & 0 deletions lux/vislib/matplotlib/MatplotlibRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from lux.vislib.matplotlib.LineChart import LineChart
from lux.vislib.matplotlib.Histogram import Histogram
from lux.vislib.matplotlib.Heatmap import Heatmap
from lux.vislib.altair.AltairRenderer import AltairRenderer
import matplotlib.pyplot as plt
from lux.utils.utils import matplotlib_setup

Expand Down Expand Up @@ -81,6 +82,8 @@ def create_vis(self, vis, standalone=True):
chart = LineChart(vis, fig, ax)
elif vis.mark == "heatmap":
chart = Heatmap(vis, fig, ax)
elif vis.mark == "geographical":
return AltairRenderer().create_vis(vis, False)
else:
chart = None
return chart
Expand Down
24 changes: 23 additions & 1 deletion tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def test_vegalite_default_actions_registered_2(global_var):
df["magnitude"] = np.random.randint(0, 20, size=len(df))
lux.config.plotting_backend = "vegalite"

# Symbol Map
# Choropleth Map
assert "Geographical" in df.recommendation
assert len(df.recommendation["Geographical"]) > 0

Expand Down Expand Up @@ -499,6 +499,28 @@ def test_matplotlib_default_actions_registered(global_var):
assert len(df.recommendation["Correlation"]) > 0


def test_matplotlib_default_actions_registered_2(global_var):
import numpy as np

df = pd.read_csv(
"https://raw.githubusercontent.com/altair-viz/vega_datasets/master/vega_datasets/_data/airports.csv"
)
df["magnitude"] = np.random.randint(0, 20, size=len(df))
lux.config.plotting_backend = "matplotlib"

# Choropleth Map
assert "Geographical" in df.recommendation
assert len(df.recommendation["Geographical"]) > 0

# Occurrence Chart
assert "Occurrence" in df.recommendation
assert len(df.recommendation["Occurrence"]) > 0

# Scatter Chart
assert "Correlation" in df.recommendation
assert len(df.recommendation["Correlation"]) > 0


def test_vegalite_heatmap_flag_config():
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/airbnb_nyc.csv")
lux.config.plotting_backend = "vegalite"
Expand Down

0 comments on commit 952d3c5

Please sign in to comment.