在 Altair 中将图像添加到图表页脚
Add image to graph footer in Altair
有没有办法添加徽标或图像,例如,在 Altair 图的页脚上?就像下图中的这个示例图像 (https://i.imgur.com/zHqeoHB.png):
import altair as alt
from vega_datasets import data
cars = data.cars()
one = alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).properties(
title = "Miles per gallon and Horsepower",
width = 300,
height = 300
)
alt.concat(one).properties(
title=alt.TitleParams(
['Source: blablabla.', '@jballesterosc_'],
baseline='bottom',
orient='bottom',
anchor='end',
fontWeight='normal',
fontSize=10
)
)
您可以使用 Layer Chart with an Image Mark 来完成此操作。例如:
import altair as alt
from vega_datasets import data
cars = data.cars()
one = alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).properties(
title = "Miles per gallon and Horsepower",
width = 300,
height = 300
)
img = alt.Chart({
"values": [{"url": "https://i.imgur.com/zHqeoHB.png"}]
}).mark_image(opacity=0.5).encode(
x=alt.value(270), x2=alt.value(300), # pixels from left
y=alt.value(320), y2=alt.value(350), # pixels from top
url="url:N"
)
alt.layer(one, img)
注意这里的x
、x2
、y
、y2
编码都指定为像素值,控制图像的水平和垂直范围.通过将它们设置为大于指定图表边界的值,图像将出现在图表轴之外。
有没有办法添加徽标或图像,例如,在 Altair 图的页脚上?就像下图中的这个示例图像 (https://i.imgur.com/zHqeoHB.png):
import altair as alt
from vega_datasets import data
cars = data.cars()
one = alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).properties(
title = "Miles per gallon and Horsepower",
width = 300,
height = 300
)
alt.concat(one).properties(
title=alt.TitleParams(
['Source: blablabla.', '@jballesterosc_'],
baseline='bottom',
orient='bottom',
anchor='end',
fontWeight='normal',
fontSize=10
)
)
您可以使用 Layer Chart with an Image Mark 来完成此操作。例如:
import altair as alt
from vega_datasets import data
cars = data.cars()
one = alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).properties(
title = "Miles per gallon and Horsepower",
width = 300,
height = 300
)
img = alt.Chart({
"values": [{"url": "https://i.imgur.com/zHqeoHB.png"}]
}).mark_image(opacity=0.5).encode(
x=alt.value(270), x2=alt.value(300), # pixels from left
y=alt.value(320), y2=alt.value(350), # pixels from top
url="url:N"
)
alt.layer(one, img)
注意这里的x
、x2
、y
、y2
编码都指定为像素值,控制图像的水平和垂直范围.通过将它们设置为大于指定图表边界的值,图像将出现在图表轴之外。