79280174

Date: 2024-12-14 06:27:29
Score: 0.5
Natty:
Report link

Yes, this can be done by setting a time unit on the x encoding and sorting by the fiscal year month. Here's an example with stock data:

enter image description here

import altair as alt
import polars as pl
from vega_datasets import data

source = data.stocks()

df = pl.DataFrame(source).filter(
    pl.col("symbol") == "AAPL", 
).with_columns(
    fy=pl.col("date").dt.year() - pl.when(pl.col("date").dt.month() <= 3).then(1).otherwise(0),
    fy_month=pl.col("date").dt.month() - 3 + pl.when(pl.col("date").dt.month() <= 3).then(12).otherwise(0)
).filter(
    pl.col("fy").is_in([2004, 2005, 2006])
)

alt.Chart(df).mark_bar().encode(
    x=alt.X("date:O", timeUnit="month", sort=alt.EncodingSortField("fy_month")),
    xOffset=alt.XOffset("fy:N"),
    y="mean(price)",
    color=alt.Color("fy:N")
).properties(width=400)
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: kgoodrick