import altair as alt
from vega_datasets import data
wheat = data.wheat()
barchart = (
alt.Chart(wheat)
.mark_bar()
.encode(
x="year:O",
y="wheat:Q",
)
.properties(width=600)
)
barchart
barchart_improved = (
(
alt.Chart(wheat)
.mark_bar(size=10)
.encode(
x=alt.X(
"year:Q",
scale=alt.Scale(domainMax=wheat.year.max()),
axis=alt.Axis(labelExpr="toString(datum.value)"),
),
y=alt.Y("wheat:Q"),
tooltip=["year", "wheat"],
)
.transform_calculate(
century=alt.expr.floor(alt.datum.year / 100) * 100,
year_short=alt.datum.year - alt.datum.century,
)
.properties(width=600)
)
.configure_axisX(labelAngle=0, ticks=False)
.configure_axisY(titleAngle=0, titleAlign="center", titleX=-10, titleY=-10)
)
barchart_improved
stocks = data.stocks()
chart = alt.Chart(stocks).encode(color=alt.Color("symbol"))
chart.mark_line().encode(x="date:T", y="price:Q")
chart = alt.Chart(stocks).encode(color=alt.Color("symbol", legend=None))
line = chart.mark_line().encode(
x=alt.X("date:T", axis=alt.Axis(title="date")),
y=alt.Y("price:Q", axis=alt.Axis(title="price")),
tooltip=["date", "price"]
)
label = chart.encode(
x=alt.X("date:T", aggregate="max"),
y=alt.Y("price:Q", aggregate={"argmax": "date"}),
text="symbol",
)
text = label.mark_text(align="left", dx=4)
circle = label.mark_circle()
(
(line + circle + text)
.configure_axisX(labelAngle=0, ticks=False)
.configure_axisY(titleAngle=0, titleAlign="center", titleX=-10, titleY=-10)
)