Python Altair 如何跳过(或挤压)x 轴的一部分?
Python Altair how do I skip (or squeeze) part of the x axis?
假设我有按日期时间索引的数据(pd.Timestamp
类型),但我确定在每天的特定时间之间没有数据(例如财务数据),我想绘制几天的数据,但我想跳过夜间时间,只绘制白天时间,这样从一天到下一天,x 轴上(夜间)不会有很大的差距。可以吗?
您可以使用与 VegaLite 答案中相同的方法 (Vega-Lite - Skip week-end (non-business days) in temporal axis);使用带有 timeUnit 编码的序数轴:
import altair as alt
import pandas as pd
records = [
{"Date": "2020/08/10 10:00", "Close": 3170},
{"Date": "2020/08/10 11:00", "Close": 3104.11},
{"Date": "2020/08/10 12:00", "Close": 3134.05},
{"Date": "2020/08/10 13:00", "Close": 3140.38},
{"Date": "2020/08/10 14:00", "Close": 3138.15},
{"Date": "2020/08/10 15:00", "Close": 3149.16},
{"Date": "2020/08/10 16:00", "Close": 3149.87},
{"Date": "2020/08/10 17:00", "Close": 3137.6},
{"Date": "2020/08/11 10:00", "Close": 3121},
{"Date": "2020/08/11 11:00", "Close": 3123.5},
{"Date": "2020/08/11 12:00", "Close": 3135.33},
{"Date": "2020/08/11 13:00", "Close": 3143.11},
{"Date": "2020/08/11 14:00", "Close": 3135.1},
{"Date": "2020/08/11 15:00", "Close": 3121.76},
{"Date": "2020/08/11 16:00", "Close": 3080.67},
{"Date": "2020/08/11 17:00", "Close": 3085.99},
{"Date": "2020/08/12 10:00", "Close": 3151.62},
{"Date": "2020/08/12 11:00", "Close": 3143.74},
{"Date": "2020/08/12 12:00", "Close": 3152.53},
{"Date": "2020/08/12 13:00", "Close": 3153.75},
{"Date": "2020/08/12 14:00", "Close": 3167.69},
{"Date": "2020/08/12 15:00", "Close": 3158.09},
{"Date": "2020/08/12 16:00", "Close": 3158.7},
{"Date": "2020/08/12 17:00", "Close": 3160.51},
{"Date": "2020/08/13 10:00", "Close": 3181},
{"Date": "2020/08/13 11:00", "Close": 3201.73},
{"Date": "2020/08/13 12:00", "Close": 3209.34},
{"Date": "2020/08/13 13:00", "Close": 3195.99},
{"Date": "2020/08/13 14:00", "Close": 3198.41},
{"Date": "2020/08/13 15:00", "Close": 3181.06},
{"Date": "2020/08/13 16:00", "Close": 3170.61},
{"Date": "2020/08/13 17:00", "Close": 3161.02},
]
alt.Chart(pd.DataFrame(records)).mark_line(point=True).encode(
x=alt.X('Date:O', timeUnit="yearmonthdatehours", axis=alt.Axis(labelAngle=-45)),
y=alt.Y('Close', scale=alt.Scale(zero=False))
)
这就是它在时间轴上的样子,请注意没有点的长连接线是夜间:
alt.Chart(pd.DataFrame(records), width=650).mark_line(point=True).encode(
x=alt.X('yearmonthdatehours(Date):T', axis=alt.Axis(labelAngle=-45)),
y=alt.Y('Close', scale=alt.Scale(zero=False))
)
假设我有按日期时间索引的数据(pd.Timestamp
类型),但我确定在每天的特定时间之间没有数据(例如财务数据),我想绘制几天的数据,但我想跳过夜间时间,只绘制白天时间,这样从一天到下一天,x 轴上(夜间)不会有很大的差距。可以吗?
您可以使用与 VegaLite 答案中相同的方法 (Vega-Lite - Skip week-end (non-business days) in temporal axis);使用带有 timeUnit 编码的序数轴:
import altair as alt
import pandas as pd
records = [
{"Date": "2020/08/10 10:00", "Close": 3170},
{"Date": "2020/08/10 11:00", "Close": 3104.11},
{"Date": "2020/08/10 12:00", "Close": 3134.05},
{"Date": "2020/08/10 13:00", "Close": 3140.38},
{"Date": "2020/08/10 14:00", "Close": 3138.15},
{"Date": "2020/08/10 15:00", "Close": 3149.16},
{"Date": "2020/08/10 16:00", "Close": 3149.87},
{"Date": "2020/08/10 17:00", "Close": 3137.6},
{"Date": "2020/08/11 10:00", "Close": 3121},
{"Date": "2020/08/11 11:00", "Close": 3123.5},
{"Date": "2020/08/11 12:00", "Close": 3135.33},
{"Date": "2020/08/11 13:00", "Close": 3143.11},
{"Date": "2020/08/11 14:00", "Close": 3135.1},
{"Date": "2020/08/11 15:00", "Close": 3121.76},
{"Date": "2020/08/11 16:00", "Close": 3080.67},
{"Date": "2020/08/11 17:00", "Close": 3085.99},
{"Date": "2020/08/12 10:00", "Close": 3151.62},
{"Date": "2020/08/12 11:00", "Close": 3143.74},
{"Date": "2020/08/12 12:00", "Close": 3152.53},
{"Date": "2020/08/12 13:00", "Close": 3153.75},
{"Date": "2020/08/12 14:00", "Close": 3167.69},
{"Date": "2020/08/12 15:00", "Close": 3158.09},
{"Date": "2020/08/12 16:00", "Close": 3158.7},
{"Date": "2020/08/12 17:00", "Close": 3160.51},
{"Date": "2020/08/13 10:00", "Close": 3181},
{"Date": "2020/08/13 11:00", "Close": 3201.73},
{"Date": "2020/08/13 12:00", "Close": 3209.34},
{"Date": "2020/08/13 13:00", "Close": 3195.99},
{"Date": "2020/08/13 14:00", "Close": 3198.41},
{"Date": "2020/08/13 15:00", "Close": 3181.06},
{"Date": "2020/08/13 16:00", "Close": 3170.61},
{"Date": "2020/08/13 17:00", "Close": 3161.02},
]
alt.Chart(pd.DataFrame(records)).mark_line(point=True).encode(
x=alt.X('Date:O', timeUnit="yearmonthdatehours", axis=alt.Axis(labelAngle=-45)),
y=alt.Y('Close', scale=alt.Scale(zero=False))
)
这就是它在时间轴上的样子,请注意没有点的长连接线是夜间:
alt.Chart(pd.DataFrame(records), width=650).mark_line(point=True).encode(
x=alt.X('yearmonthdatehours(Date):T', axis=alt.Axis(labelAngle=-45)),
y=alt.Y('Close', scale=alt.Scale(zero=False))
)