gpt4 book ai didi

python - Altair - 在图中画一条线,其中 x = y

转载 作者:行者123 更新时间:2023-12-05 01:35:29 25 4
gpt4 key购买 nike

这段代码:

chart = alt.Chart(df).mark_point(filled=True).encode(
alt.X('Goals Conceded:Q'),
alt.Y('Goals:Q'),
alt.Size('Goals:Q', legend=None, scale=alt.Scale(range=[0, 1500])),
alt.Color('Color', legend=None, scale=None),
tooltip = [alt.Tooltip('For Team:N'),
alt.Tooltip('Goals:Q'),
alt.Tooltip('Goals Conceded:Q')]
).properties(
width=800,
height=600
)

情节:

enter image description here

现在我想手动添加一行,其中 x = y,以获得以下结果:

enter image description here


我该怎么做?

最佳答案

你可以添加一个虚拟行:

line = pd.DataFrame({
'Goals Conceded': [0, 2],
'Goals': [0, 2],
})

line_plot = alt.Chart(line).mark_line(color= 'red').encode(
x= 'Goals Conceded',
y= 'Goals'.
)

chart + line_plot

我没有你的数据集,所以下面是一个主要来自 Altair Example Gallery 的例子:

import pandas as pd
import altair as alt
from vega_datasets import data

source = data.iris()

iris_plot = alt.Chart(source).mark_circle().encode(
alt.X('sepalLength'),
alt.Y('sepalWidth'),
color='species',
size='petalWidth'
)

line = pd.DataFrame({
'sepalLength': [0, 5],
'sepalWidth': [0, 5],
})

line_plot = alt.Chart(line).mark_line(color= 'red').encode(
x= 'sepalLength',
y= 'sepalWidth',
)

iris_plot + line_plot

enter image description here

关于python - Altair - 在图中画一条线,其中 x = y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62854174/

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