Altair Chart "reads" 来自 TimeStamps 的信息多于应有的信息

Altair Chart "reads" more information from TimeStamps than it should

大家

我有一个如下所示的 DataFrame(我通过 df.to_dict() 得到的):

{'end_datum': {48: Timestamp('2020-09-25 00:00:00'),
  96: Timestamp('2020-09-30 00:00:00'),
  77: Timestamp('2020-09-29 00:00:00'),
  51: Timestamp('2020-09-25 00:00:00'),
  95: Timestamp('2020-09-30 00:00:00')},
 'type': {48: 'Frist Gericht',
  96: 'Frist Gericht',
  77: 'Berufungsfrist',
  51: 'Frist Gericht',
  95: 'Frist Gericht'},
 'motorenbezeichnung': {48: 'EA 189',
  96: 'EA 288',
  77: 'OM 642',
  51: 'OM 651',
  95: 'EA 189'},
 'size': {48: 30, 96: 9, 77: 1, 51: 8, 95: 54}}

我用以下代码绘制了一张 Altair 图表:

alt.Chart(pv_clean).mark_bar().encode(
    x= "end_datum:T",
    y="size:Q",
    color = "motorenbezeichnung:N").properties(width=1000)

我得到了以下输出:

X 轴上的标签包含了很多信息。我试过使用 altair 的 transform_timeunit(),

alt.Chart(pv_clean).mark_bar().encode(
    y= "date(end_datum:T)",
    x="size:Q",
    color = "motorenbezeichnung:N").properties(width=1000)

但图表变为空白:

有趣的是,如果我将日期从 x 轴更改为 y 轴,

alt.Chart(pv_clean).mark_bar().encode(
    y= "end_datum:T",
    x="size:Q",
    color = "motorenbezeichnung:N").properties(width=1000)

日期的解析方式更有意义:

任何人都可以向我解释以下内容:

  1. 如何获取X轴显示的月份和日期?

  2. 为什么 Altair 图表中的 x 轴和 y 轴对日期的解释不同?

  3. 为什么当我尝试转换时间单位时图表变成空白?

我使用的是 jupyter lab 2.2.6 版和 Altair 4.1.0 版。

提前致谢。

问题是 x 轴上的默认刻度数比您想要的要多。

解决此问题的一种简便方法是将报价之间的最小值 space 设置为您希望看到的值;对于时间轴,刻度为 spaced 毫秒,因此使用

x=alt.X("end_datum:T", axis=alt.Axis(tickMinStep=1000*60*60*24))

让事情看起来好一点。

这是一个完整的示例,使用您提供的数据:

alt.Chart(pv_clean).mark_bar().encode(
    x=alt.X("end_datum:T", axis=alt.Axis(tickMinStep=1000*60*60*24)),
    y="size:Q",
    color = "motorenbezeichnung:N"
).properties(width=1000)

请注意,您的图表 2 不起作用,因为您写的是 y= "date(end_datum:T)" 而不是 y= "date(end_datum):T",而图表 3 确实起作用,因为 y-axis 跨越的像素较少,导致默认值较少ticks.