admin管理员组文章数量:1318021
Using a data frame of dates and values starting from 1 Jan 2022:
import datetime as dt
import altair as alt
import polars as pl
import numpy as np
alt.renderers.enable("browser")
dates = pl.date_range(dt.date(2022, 1, 1), dt.date(2025, 1, 22), "1d", eager = True)
values = np.random.uniform(size = len(dates))
df = pl.DataFrame({"dates": dates, "values": values})
alt.Chart(df).mark_point().encode(alt.X("dates:T"), alt.Y("values:Q")).show()
But if I start the data frame from 2020 and filter it for dates > 1 Jan 2022:
dates_b = pl.date_range(dt.date(2020, 1, 1), dt.date(2025, 1, 22), "1d", eager = True)
values_b = np.random.uniform(size = len(dates_b))
df_b = pl.DataFrame({"dates": dates, "values": values})
alt.Chart(df_b.filter(pl.col("dates") > dt.date(2022, 1, 1))).mark_point().encode(alt.X("dates:T"), alt.Y("values:Q")).show()
How can I specify that years must be shown?
Note that I do get the right result if I filter using >=
to include 1 Jan 2022, but that's besides the point. I always need years.
Using a data frame of dates and values starting from 1 Jan 2022:
import datetime as dt
import altair as alt
import polars as pl
import numpy as np
alt.renderers.enable("browser")
dates = pl.date_range(dt.date(2022, 1, 1), dt.date(2025, 1, 22), "1d", eager = True)
values = np.random.uniform(size = len(dates))
df = pl.DataFrame({"dates": dates, "values": values})
alt.Chart(df).mark_point().encode(alt.X("dates:T"), alt.Y("values:Q")).show()
But if I start the data frame from 2020 and filter it for dates > 1 Jan 2022:
dates_b = pl.date_range(dt.date(2020, 1, 1), dt.date(2025, 1, 22), "1d", eager = True)
values_b = np.random.uniform(size = len(dates_b))
df_b = pl.DataFrame({"dates": dates, "values": values})
alt.Chart(df_b.filter(pl.col("dates") > dt.date(2022, 1, 1))).mark_point().encode(alt.X("dates:T"), alt.Y("values:Q")).show()
How can I specify that years must be shown?
Note that I do get the right result if I filter using >=
to include 1 Jan 2022, but that's besides the point. I always need years.
1 Answer
Reset to default 2You can use labelExpr
to build your own logic for setting tick labels. For example, this gives the year if the month is January and the month otherwise.
dates_b = pl.date_range(dt.date(2020, 1, 1), dt.date(2025, 1, 22), "1d", eager=True)
values_b = np.random.uniform(size=len(dates_b))
df_b = pl.DataFrame({"dates": dates, "values": values})
alt.Chart(df_b.filter(pl.col("dates") > dt.date(2022, 1, 1))).mark_point().encode(
alt.X("dates:T").axis(
labelExpr="timeFormat(datum.value, '%m') == '01' ? timeFormat(datum.value, '%Y') : timeFormat(datum.value, '%b')",
),
alt.Y("values:Q"),
)
本文标签: pythonForce Altair chart to display yearsStack Overflow
版权声明:本文标题:python - Force Altair chart to display years - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742033846a2416957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论