我正在尝试使工具提示“百分比”成为实际百分比而不是小数。即使我包含 alt.Tooltip('Percentage:Q',format='.2%')
,它似乎也不起作用。
此外,我正在尝试使图例比例从 0-100% 而不是 40-70%。
如有任何帮助,我们将不胜感激!
import altair as alt
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
variable_list = ['Percentage', 'State Name', 'state_id']
alt.Chart(states).mark_geoshape().encode(
color=alt.Color('Percentage:Q', title='Positive NFB', legend=alt.Legend(format=".0%"), scale=alt.Scale(scheme='yellowgreen')),
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]).properties(title="Percentage of People in Households with Positive NFB"
).transform_lookup(
lookup='id',
from_=alt.LookupData(states_positive_NFB, 'state_id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
)
最佳答案
要更改色标的域,您可以将
domain
参数传递给alt.Scale()
:例如alt.Scale(scheme='yellowgreen', domain=[0, 1])
要显示工具提示格式,您可以删除重复的工具提示编码,因为第一个似乎优先。也就是说,而不是
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]
你应该使用
tooltip=['State Name:N', alt.Tooltip('Percentage:Q', format='.2%')]
关于tooltip - 格式化 Altair 等值线图图例比例和工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65187827/