gpt4 book ai didi

python - 向数据点添加 X-Y 偏移

转载 作者:行者123 更新时间:2023-12-01 00:13:33 24 4
gpt4 key购买 nike

我正在寻找一种方法来指定绘制数据点的 X-Y 偏移。我刚刚开始接触 Altair,所以请耐心等待。

情况:我有一个记录 30 人每日测量数据的数据集。每个人每天都可以注册几种不同类型的测量结果。

示例数据集和绘图,有 2 个人和 2 种测量类型:

import pandas as pd

df = pd.DataFrame.from_dict({"date": pd.to_datetime(pd.date_range("2019-12-01", periods=5).repeat(4)),
"person": pd.np.tile(["Bob", "Amy"], 10),
"measurement_type": pd.np.tile(["score_a", "score_a", "score_b", "score_b"], 5),
"value": 20.0*np.random.random(size=20)})

import altair as alt

alt.Chart(df, width=600, height=100) \
.mark_circle(size=150) \
.encode(x = "date",
y = "person",
color = alt.Color("value"))

这给了我这个图表: Two measurement types on top of each other...

在上面的示例中,两种测量类型绘制在彼此之上。我想根据“measurement_type”列向圆圈添加偏移量,以便它们都可以在图表中的日期人员位置周围可见。

这是我想要实现的目标的模型: Non-blocking measurement types!

我一直在搜索文档,但还没有弄清楚如何做到这一点 - 一直在尝试使用“stack”选项以及dxdy选项, ...我有一种感觉,这应该只是另一个编码 channel (offset 或类似的),但这并不存在。

有人能指出我正确的方向吗?

最佳答案

目前 Altair 中没有偏移编码的概念,因此最好的方法是将列编码与 y 编码相结合,类似于 Grouped Bar Chart Altair 文档中的示例:

alt.Chart(df,
width=600, height=100
).mark_circle(
size=150
).encode(
x = "date",
row='person',
y = "measurement_type",
color = alt.Color("value")
)

enter image description here

然后您可以使用标准 chart configuration 微调结果的外观设置:

alt.Chart(df,
width=600, height=alt.Step(25)
).mark_circle(
size=150
).encode(
x = "date",
row='person',
y = alt.Y("measurement_type", title=None),
color = alt.Color("value")
).configure_facet(
spacing=10
).configure_view(
strokeOpacity=0
)

enter image description here

关于python - 向数据点添加 X-Y 偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59467230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com