[英]How can I exclude certain dates (e.g., weekends) from time series plots?
In the following example, I'd like to exclude weekends and plot Y
as a straight line, and specify some custom frequency for major tick labels since they would be a "broken" time series (eg, every Monday, a la matplotlib
's set_major_locator
). 在下面的例子中,我想排除周末并将
Y
绘制成一条直线,并为主要刻度标签指定一些自定义频率,因为它们将是一个“破碎”的时间序列(例如,每周一,一个la matplotlib
的set_major_locator
)。
How would I do that in Altair? 我怎么会在Altair那样做?
import altair as alt
import pandas as pd
index = pd.date_range('2018-01-01', '2018-01-31', freq='B')
df = pd.DataFrame(pd.np.arange(len(index)), index=index, columns=['Y'])
alt.Chart(df.reset_index()).mark_line().encode(
x='index',
y='Y'
)
A quick way to do that is to specify the axis as an ordinal field. 快速执行此操作的方法是将轴指定为序数字段。 This would produce a very ugly axis, with the hours specified for every tick.
这将产生一个非常难看的轴,每个刻度都指定了小时数。 To change that, I add a column to the dataframe with a given label.
要更改它,我会使用给定标签向数据框添加一列。 I also added the
grid
, as by default it is removed for an ordinal encoding, and set the labelAngle
to 0. 我还添加了
grid
,因为默认情况下它会被删除以进行序数编码,并将labelAngle
设置为0。
df2 = df.assign(label=index.strftime('%b %d %y'))
alt.Chart(df2).mark_line().encode(
x=alt.X('label:O', axis=alt.Axis(grid=True, labelAngle=0)),
y='Y:Q'
)
Beware that it would remove any missing point. 请注意它会删除任何遗漏点。 So, maybe you want to add a tooltip.
所以,也许你想添加一个工具提示。 This is discussed in the documentation here .
这将在此处的文档中讨论。 You can also play with
labelOverlap
in the axis setting depending of hat you want. 您还可以根据所需的帽子在轴设置中使用
labelOverlap
。
To customize the axis, we can build one up using mark_text
and bring back the grid with mark_rule
and a custom dataframe. 要自定义轴,我们可以使用
mark_text
构建一个轴,并使用mark_text
和自定义数据mark_rule
恢复网格。 It does not necessarily scale up well, but it can give you some ideas. 它不一定能很好地扩展,但它可以给你一些想法。
df3 = df2.loc[df2.index.dayofweek == 0, :].copy()
df3["Y"] = 0
text_chart = alt.Chart(df3).mark_text(dy = 15).encode(
x=alt.X('label:O', axis = None),
y=alt.Y('Y:Q'),
text=alt.Text('label:O')
)
tick_chart = alt.Chart(df3).mark_rule(color='grey').encode(
x=alt.X('label:O', axis=None),
)
line_chart = alt.Chart(df2).mark_line().encode(
x=alt.X('label:O', axis=None, scale=alt.Scale(rangeStep=15)),
y='Y:Q'
)
text_chart + tick_chart + line_chart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.