Skip to content

Commit

Permalink
Fixed Wheat and Wages case study (#3086)
Browse files Browse the repository at this point in the history
* fixed wheat and wages case study

* aligned code style of chart to make it better readable

* Rewrite dictionaries as keyword arguments

* Change bin='binned' to method-syntax .bin('binned')

---------

Co-authored-by: Stefan Binder <[email protected]>
  • Loading branch information
thomend and binste authored Jun 18, 2023
1 parent 6d130e3 commit 564b472
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
62 changes: 39 additions & 23 deletions tests/examples_arguments_syntax/wheat_wages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,67 @@
"""
# category: case studies
import altair as alt
import pandas as pd
from vega_datasets import data


base_wheat = alt.Chart(data.wheat.url).transform_calculate(
year_end="+datum.year + 5")
base_wheat = alt.Chart(data.wheat.url).transform_calculate(year_end="+datum.year + 5")

base_monarchs = alt.Chart(data.monarchs.url).transform_calculate(
offset="((!datum.commonwealth && datum.index % 2) ? -1: 1) * 2 + 95",
off2="((!datum.commonwealth && datum.index % 2) ? -1: 1) + 95",
y="95",
x="+datum.start + (+datum.end - +datum.start)/2"
x="+datum.start + (+datum.end - +datum.start)/2",
)

bars = base_wheat.mark_bar(**{"fill": "#aaa", "stroke": "#999"}).encode(
x=alt.X("year:Q", axis=alt.Axis(format='d', tickCount=5)),
bars = base_wheat.mark_bar(fill="#aaa", stroke="#999").encode(
x=alt.X("year:Q", bin="binned", axis=alt.Axis(format="d", tickCount=5)).scale(
zero=False
),
y=alt.Y("wheat:Q", axis=alt.Axis(zindex=1)),
x2=alt.X2("year_end")
x2=alt.X2("year_end"),
)

area = base_wheat.mark_area(**{"color": "#a4cedb", "opacity": 0.7}).encode(
x=alt.X("year:Q"),
y=alt.Y("wages:Q")
section_data = pd.DataFrame(
[
{"year": 1600},
{"year": 1650},
{"year": 1700},
{"year": 1750},
{"year": 1800},
]
)

area_line_1 = area.mark_line(**{"color": "#000", "opacity": 0.7})
area_line_2 = area.mark_line(**{"yOffset": -2, "color": "#EE8182"})
section_line = (
alt.Chart(section_data)
.mark_rule(stroke="#000", strokeWidth=0.6, opacity=0.7)
.encode(x=alt.X("year"))
)

area = base_wheat.mark_area(color="#a4cedb", opacity=0.7).encode(
x=alt.X("year:Q"), y=alt.Y("wages:Q")
)

area_line_1 = area.mark_line(color="#000", opacity=0.7)
area_line_2 = area.mark_line(yOffset=-2, color="#EE8182")

top_bars = base_monarchs.mark_bar(stroke="#000").encode(
x=alt.X("start:Q"),
x2=alt.X2("end"),
y=alt.Y("y:Q"),
y2=alt.Y2("offset"),
fill=alt.Fill("commonwealth:N", legend=None, scale=alt.Scale(range=["black", "white"]))
fill=alt.Fill(
"commonwealth:N", legend=None, scale=alt.Scale(range=["black", "white"])
),
)

top_text = base_monarchs.mark_text(**{"yOffset": 14, "fontSize": 9, "fontStyle": "italic"}).encode(
x=alt.X("x:Q"),
y=alt.Y("off2:Q"),
text=alt.Text("name:N")
top_text = base_monarchs.mark_text(yOffset=14, fontSize=9, fontStyle="italic").encode(
x=alt.X("x:Q"), y=alt.Y("off2:Q"), text=alt.Text("name:N")
)

(bars + area + area_line_1 + area_line_2 + top_bars + top_text).properties(
width=900, height=400
).configure_axis(
title=None, gridColor="white", gridOpacity=0.25, domain=False
).configure_view(
stroke="transparent"
)
(
(bars + section_line + area + area_line_1 + area_line_2 + top_bars + top_text)
.properties(width=900, height=400)
.configure_axis(title=None, gridColor="white", gridOpacity=0.25, domain=False)
.configure_view(stroke="transparent")
)
56 changes: 34 additions & 22 deletions tests/examples_methods_syntax/wheat_wages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,63 @@
"""
# category: case studies
import altair as alt
import pandas as pd
from vega_datasets import data


base_wheat = alt.Chart(data.wheat.url).transform_calculate(
year_end="+datum.year + 5")
base_wheat = alt.Chart(data.wheat.url).transform_calculate(year_end="+datum.year + 5")

base_monarchs = alt.Chart(data.monarchs.url).transform_calculate(
offset="((!datum.commonwealth && datum.index % 2) ? -1: 1) * 2 + 95",
off2="((!datum.commonwealth && datum.index % 2) ? -1: 1) + 95",
y="95",
x="+datum.start + (+datum.end - +datum.start)/2"
x="+datum.start + (+datum.end - +datum.start)/2",
)

bars = base_wheat.mark_bar(**{"fill": "#aaa", "stroke": "#999"}).encode(
alt.X("year:Q").axis(format='d', tickCount=5),
bars = base_wheat.mark_bar(fill="#aaa", stroke="#999").encode(
alt.X("year:Q").bin("binned").axis(format="d", tickCount=5).scale(zero=False),
alt.Y("wheat:Q").axis(zindex=1),
alt.X2("year_end")
alt.X2("year_end"),
)

area = base_wheat.mark_area(**{"color": "#a4cedb", "opacity": 0.7}).encode(
alt.X("year:Q"),
alt.Y("wages:Q")
section_data = pd.DataFrame(
[
{"year": 1600},
{"year": 1650},
{"year": 1700},
{"year": 1750},
{"year": 1800},
]
)

area_line_1 = area.mark_line(**{"color": "#000", "opacity": 0.7})
area_line_2 = area.mark_line(**{"yOffset": -2, "color": "#EE8182"})
section_line = (
alt.Chart(section_data)
.mark_rule(stroke="#000", strokeWidth=0.6, opacity=0.7)
.encode(alt.X("year"))
)

area = base_wheat.mark_area(color="#a4cedb", opacity=0.7).encode(
alt.X("year:Q"), alt.Y("wages:Q")
)

area_line_1 = area.mark_line(color="#000", opacity=0.7)
area_line_2 = area.mark_line(yOffset=-2, color="#EE8182")

top_bars = base_monarchs.mark_bar(stroke="#000").encode(
alt.X("start:Q"),
alt.X2("end"),
alt.Y("y:Q"),
alt.Y2("offset"),
alt.Fill("commonwealth:N").legend(None).scale(range=["black", "white"])
alt.Fill("commonwealth:N").legend(None).scale(range=["black", "white"]),
)

top_text = base_monarchs.mark_text(**{"yOffset": 14, "fontSize": 9, "fontStyle": "italic"}).encode(
alt.X("x:Q"),
alt.Y("off2:Q"),
alt.Text("name:N")
top_text = base_monarchs.mark_text(yOffset=14, fontSize=9, fontStyle="italic").encode(
alt.X("x:Q"), alt.Y("off2:Q"), alt.Text("name:N")
)

(bars + area + area_line_1 + area_line_2 + top_bars + top_text).properties(
width=900, height=400
).configure_axis(
title=None, gridColor="white", gridOpacity=0.25, domain=False
).configure_view(
stroke="transparent"
(
(bars + section_line + area + area_line_1 + area_line_2 + top_bars + top_text)
.properties(width=900, height=400)
.configure_axis(title=None, gridColor="white", gridOpacity=0.25, domain=False)
.configure_view(stroke="transparent")
)

0 comments on commit 564b472

Please sign in to comment.