当我用 altair 在 x 轴上绘制时间时,它不显示年份。有可能解决这个问题吗?

When I plot time on the x axis with altair it doesn't show year. Is it possible to fix this?

我非常喜欢使用 altair。但是,有时当我绘制时间时,它不显示年份而只显示月份。我尝试更改尺寸但仍然无法获得它,除非宽度太大以至于使图像不那么用户友好。

import altair as alt
df_all['Date'] = pd.to_datetime(df_all['Date'])

alt.Chart(df_all).mark_line().encode(
    x='Date:T',
    y='Values',
    color='Key',
).properties(height=500, width=600)

数据有这些类型:

Date      datetime64[ns]
Key               object
Values           float64

看起来像这样:

Date,Key,Values
2020-01-12,Leisure - Family,100.000000
2020-01-12,Leisure - Non-Family,100.000000
2020-01-12,Business,100.000000
2020-01-19,Leisure - Family,101.989826
2020-01-19,Business,95.664472

输出如下所示:

https://imgur.com/a/D9AlvLj

提前致谢!

您可以change the axis format to any D3 time format,例如:

import pandas as pd
import altair as alt


df = pd.read_clipboard(sep=',', parse_dates=['Date'])

alt.Chart(df).mark_line().encode(
    x=alt.X('Date', axis=alt.Axis(format='%e %b, %Y')),
    y=alt.Y('Values', scale=alt.Scale(zero=False)),
    color='Key')