与我添加文本作为图中点的标签相比,我更喜欢工具提示的外观,有没有办法使其无论鼠标是否在其上都可见?
我查了一下,但没有找到任何解决方案,也许是搞乱了条件?
如果您有想要测试的想法,请查看文档中的示例代码:
import altair as alt
from vega_datasets import data
source = data.cars()
alt.Chart(source).mark_circle(size=60).encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).interactive()
谢谢你:)
最佳答案
根据我上面的评论,我认为最简单的方法是添加一个样式化的文本框。您可以看到an example of how to style it in this issue ,我也粘贴在下面:
import altair as alt
from vega_datasets import data
cars = data.cars()
chart = alt.Chart(cars).mark_circle().encode(
alt.X('Miles_per_Gallon', scale=alt.Scale(domain=(5,50))),
y='Weight_in_lbs')
corl = cars[['Miles_per_Gallon','Weight_in_lbs']].corr().iloc[0,1]
text = alt.Chart({'values':[{}]}).mark_text(
align="left", baseline="top"
).encode(
x=alt.value(5), # pixels from left
y=alt.value(5), # pixels from top
text=alt.value([f"r: {corl:.3f}", 'Line 2']))
box = alt.Chart({'values':[{}]}).mark_rect(stroke='black', color='orange').encode(
x=alt.value(3),
x2=alt.value(50),
y=alt.value(3),
y2=alt.value(30))
chart + box + text + chart.transform_regression('Miles_per_Gallon','Weight_in_lbs').mark_line()
关于altair - 有没有办法一直显示 Altair Tooltip?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73728897/