我真的很喜欢使用 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
输出如下:
提前非常感谢!
最佳答案
您可以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')
关于python - 当我用 Altair 在 x 轴上绘制时间时,它不显示年份。有可能解决这个问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67854186/