Altair:在工具提示中显示当前点的值

huangapple go评论51阅读模式
英文:

Altair: showing the value of the current point in the tooltip

问题

在下面的代码中,我们有一个数据集,可以理解为:“两位厨师cook1cook2正在进行比赛。他们必须制作四道菜,每次使用两种给定的食材ingredient1ingredient2。评审对这些菜进行了评分,分数存储在_score中。

我想使用Altair来显示一个图表,其中x轴是每道菜(1、2、3、4),y轴分别包含两位厨师的分数。目前这个图表可以工作,但主要问题是,在悬停时,工具提示不包括悬停的当前点的分数。

import altair as alt
import pandas as pd

df = pd.DataFrame({
    "ingredient1": ["potato", "onion", "carrot", "beet"],
    "ingredient2": ["tomato", "pepper", "zucchini", "lettuce"],
    "dish": [1, 2, 3, 4],
    "cook1": ["cook1 dish1", "cook1 dish2", "cook1 dish3", "cook1 dish4"],
    "cook1_score": [0.4, 0.3, 0.7, 0.9],
    "cook2": ["cook2 dish1", "cook2 dish2", "cook2 dish3", "cook2 dish4"],
    "cook2_score": [0.6, 0.2, 0.5, 0.6],
})

value_vars = [c for c in df.columns if c.endswith("_score")]
cook_names = [c.replace("_score", "") for c in value_vars]
id_vars = ["dish", "ingredient1", "ingredient2",] + cook_names
df_melt = df.melt(id_vars=id_vars, value_vars=value_vars,
                  var_name="cook", value_name="score")

chart = alt.Chart(df_melt).mark_circle().encode(
    x=alt.X("dish:O", title="Dish number"),
    y=alt.Y("score:Q", title="Score"),
    color="cook:N",
    tooltip=id_vars
)

chart.show()

我尝试显式将分数列添加到工具提示中:

    tooltip=id_vars+value_vars

但会产生以下错误:

ValueError: cook1_score encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.

那么,我如何让Altair也显示(仅)当前悬停元素的分数呢?

英文:

In the code below, we have a dataset that can be read as: "two cooks cook1, cook2 are doing a competition. They have to make four dishes, each time with two given ingredients ingredient1, ingredient2. A jury has scored the dishes and the grades are stored in _score.

I want to use Altair to show a graph where the x-axis is each dish (1, 2, 3, 4) and the y-axis contains the scores of the two cooks separately. This currently works but the main issue is that on hover, the tooltip does not include the score of the current point that is being hovered.

import altair as alt
import pandas as pd


df = pd.DataFrame({
    "ingredient1": ["potato", "onion", "carrot", "beet"],
    "ingredient2": ["tomato", "pepper", "zucchini", "lettuce"],
    "dish": [1, 2, 3, 4],
    "cook1": ["cook1 dish1", "cook1 dish2", "cook1 dish3", "cook1 dish4"],
    "cook1_score": [0.4, 0.3, 0.7, 0.9],
    "cook2": ["cook2 dish1", "cook2 dish2", "cook2 dish3", "cook2 dish4"],
    "cook2_score": [0.6, 0.2, 0.5, 0.6],
})


value_vars = [c for c in df.columns if c.endswith("_score")]
cook_names = [c.replace("_score", "") for c in value_vars]
id_vars = ["dish", "ingredient1", "ingredient2",] + cook_names
df_melt = df.melt(id_vars=id_vars, value_vars=value_vars,
                  var_name="cook", value_name="score")

chart = alt.Chart(df_melt).mark_circle().encode(
    x=alt.X("dish:O", title="Dish number"),
    y=alt.Y("score:Q", title="Score"),
    color="cook:N",
    tooltip=id_vars
)

chart.show()

I tried explicitly adding the score columns to the tooltip:

    tooltip=id_vars+value_vars

But that yields the following error:

> ValueError: cook1_score encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.

So how can I get altair to also show the score of (only) the currently hovered element?

答案1

得分: 1

cook1_score 不是 df_melt 中的一列,这就是为什么你看到错误。设置 tooltip=id_vars+['score'] 将起作用。

英文:

cook1_score is not a column in df_melt, which is why you see the error. Setting tooltip=id_vars+['score'] will work.

huangapple
  • 本文由 发表于 2023年6月2日 01:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定