【问题标题】:How do I limit Altair tooltip to only two numbers after the decimal point?如何将 Altair 工具提示限制为小数点后仅两个数字?
【发布时间】:2021-07-30 01:12:19
【问题描述】:

【问题讨论】:

  • 在此处查看example。我没有你的代码,所以我猜,但我认为以下代码支持是可能的。 alt.Tooltip(..., format='.2f')

标签: python jupyter-notebook altair


【解决方案1】:

您可以使用alt.Tooltipformat 参数来自定义工具提示格式。它接受d3-format 字符串,它允许各种可能的规范(包括小数点、百分比、日期/时间、货币等)。如果您使用format='.2f',它会将工具提示格式化为小数点后两位数的浮点数。例如:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'name': ['A', 'B', 'C'],
    'value': [2.548493, 3.6284932, 4.7148392]
})

alt.Chart(df).mark_bar().encode(
    y='name',
    x='value',
    tooltip=alt.Tooltip('value', format='.2f')
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2017-04-01
    • 2021-04-23
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多