gpt4 book ai didi

python-3.x - 如何将一个盒子形状的图层添加到 Altair 图中?

转载 作者:行者123 更新时间:2023-12-04 15:29:13 25 4
gpt4 key购买 nike

我正在尝试使用带有坐标的 Pandas 数据框添加一个用作罢工区的框,并将其传递给 altair。

box = pd.DataFrame()
box.loc[:,"x"] = [-0.5, 0.5, 0.5, -0.5]
box.loc[:,'y'] = [1.25, 1.25, 0.5, 0.5]

我尝试了以下方法:
g = alt.Chart(box.loc[0:1,:]).mark_line().encode(
x = 'x',
y = 'y')

d = alt.Chart(box.loc[1:2,:]).mark_line().encode(
x = 'x',
y = 'y')

e = alt.Chart(box.loc[2:3,:]).mark_line().encode(
x = 'x',
y = 'y')

f = alt.Chart(box.loc[3:4,:]).mark_line().encode(
x = 'x',
y = 'y')

g + d + e + f

我还想知道如何调整 x 和 y 轴,以便框周围有一点边距?

最佳答案

我建议用一个折线图绘制所有四个边。然后您可以使用 domain scale 参数来调整轴限制(更多信息请参见 Altair 文档的 Adjusting Axis Limits 部分)。

下面是一个例子:

import altair as alt
import pandas as pd

box = pd.DataFrame({
'x': [-0.5, 0.5, 0.5, -0.5, -0.5],
'y': [1.25, 1.25, 0.5, 0.5, 1.25]
}).reset_index()


alt.Chart(box).mark_line().encode(
alt.X('x', scale=alt.Scale(domain=(-1, 1))),
alt.Y('y', scale=alt.Scale(domain=(0, 1.5))),
order='index'
)

enter image description here

或者,您可以使用 rect标记以避免必须以正确的顺序手动构建矩形的坐标:
box = pd.DataFrame({'x1': [-0.5], 'x2': [0.5], 'y1': [0.5], 'y2': [1.25]})

alt.Chart(box).mark_rect(fill='none', stroke='black').encode(
alt.X('x1', scale=alt.Scale(domain=(-1, 1))),
alt.Y('y1', scale=alt.Scale(domain=(0, 1.5))),
x2='x2',
y2='y2'
)

enter image description here

关于python-3.x - 如何将一个盒子形状的图层添加到 Altair 图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015304/

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