gpt4 book ai didi

python - 如何在 Altair 的一行中添加文本?

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

我想在这些引用行中添加文本,以下是我使用的代码:

double2 = alt.Chart(source2).mark_line().transform_calculate(
double2='5*pow(2,(datum.x/2))'
).transform_fold(
['double2']
).encode(
x='x:Q',
y=alt.Y('value:Q', scale=alt.Scale(type='log')),
color=alt.value('lightgray')
)

source5 = alt.sequence(start=0, stop=28, step=1, as_='x')

double5 = alt.Chart(source5).mark_line().transform_calculate(
double5='5*pow(2,(datum.x/5))'
).transform_fold(
['double5']
).encode(
x='x:Q',
y='value:Q',
color=alt.value('lightgray')
)

double2 + double5

output

我想沿着“每 2 天死亡人数翻倍”和“每 5 天”这一行添加文字,如下面的《我们的世界数据》图表所示:

our world in data

最佳答案

没有任何好的自动化方法可以做到这一点,因为 Vega-Lite 中的文本角度不能绑定(bind)到数据坐标。但是通过一些调整,您可以使用文本层完成此操作:

import altair as alt

source2 = alt.sequence(start=0, stop=28, step=1, as_='x')

double2 = alt.Chart(source2).mark_line().transform_calculate(
double2='5*pow(2,(datum.x/2))'
).transform_fold(
['double2']
).encode(
x='x:Q',
y=alt.Y('value:Q', scale=alt.Scale(type='log')),
color=alt.value('lightgray')
)

source5 = alt.sequence(start=0, stop=28, step=1, as_='x')

double5 = alt.Chart(source5).mark_line().transform_calculate(
double5='5*pow(2,(datum.x/5))'
).transform_fold(
['double5']
).encode(
x='x:Q',
y='value:Q',
color=alt.value('lightgray')
)

text5 = alt.Chart({'values':[{'x': 20, 'y': 100}]}).mark_text(
text='doubles every 5 days', angle=346
).encode(
x='x:Q', y='y:Q'
)

text2 = alt.Chart({'values':[{'x': 20, 'y': 7000}]}).mark_text(
text='doubles every 2 days', angle=327
).encode(
x='x:Q', y='y:Q'
)

double2 + double5 + text2 + text5

enter image description here

一旦支持新的数据编码,这在 Altair 4.2 中会更清晰一些。

关于python - 如何在 Altair 的一行中添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289608/

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