简体   繁体   English

如何从时间序列图中排除某些日期(例如,周末)?

[英]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 matplotlibset_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.

相关问题 如何从时间序列数据中提取有用的功能(例如,用户在论坛中的日常活动) - How to extract useful features from time-series data (e.g., users' daily activities in a forum) 计算在给定日期范围内时间经过某个检查点的天数,例如在酒店的住宿次数 - Count the number of day if time pass a certain checkpoint in a given range of dates, e.g. number of stay in hotel 使用系列作为输入,如何在 Pandas 数据框中找到具有匹配值的行? 例如df.loc[系列]? - Using a series as input, how can I find rows with matching values in a pandas dataframe? e.g. df.loc[series]? 如何测试pandas.Series是否仅包含某些类型(例如int)? - How to test whether pandas.Series contains only certain type (e.g. int)? Plot 例如时间序列数据中一个月的最大值 - Plot e.g. max value of a month in time series data 如何在 Tensorboard(例如 Matplotlib Plots)中显示自定义图像? - How to Display Custom Images in Tensorboard (e.g. Matplotlib Plots)? 如何进行时间序列反向重采样,例如从最后一个数据日期开始的 5 个工作日? - How to do time series backward resampling e.g. 5 business days starting on the last data date? 每当例如分类 pandas 时间序列更改 state 时如何提取时间戳 - How to extract the timestamps whenever an e.g. categorical pandas time series changes state 如何检查是否是某个时间,例如下午 2:00 - How to check if it's a certain time like e.g. 2:00pm 如何将字符串(例如'A')引用到更大列表的索引(例如['A','B','C','D',...])? - How can I reference a string (e.g. 'A') to the index of a larger list (e.g. ['A', 'B', 'C', 'D', ...])?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM