以下是我想在Altair图上显示的工具提示和标签的三个问题。所有问题或多或少都联系在一起。
Year而不是properties.annee
Region而不是properties.region
生物能源代替properties.bioenerie ...
其次,我想舍入工具提示中显示的值。“ 11.2”而不是“ 11.1687087653”我编写的代码可以为我在区域中放置的标签提供所需的功能,但不适用于工具提示。
第三,我想在标签和工具提示中显示单位,但在文档中找不到正确的语法。
下面是我的代码。
提前感谢您的回答。
伯特兰
def gen_map(data: gpd.geodataframe.GeoDataFrame, title: str, abs_values: bool):
data_json = json.loads(data.to_json())
choro_data = alt.Data(values=data_json['features'])
# Absolute values or relative values
if abs_values:
column = data.columns[0]
units = 'MW'
form = '.0f'
else:
column = data.columns[1]
units = '%'
form = '.1f'
# Base layer
layer = alt.Chart(choro_data, title=title).mark_geoshape(
stroke='white',
strokeWidth=1
).encode(
alt.Color(f'properties.{column}:Q',
type='quantitative',
title = f'Installed Capacity in {units}'),
tooltip=[f'properties.annee:Q',
f'properties.region:O',
f'properties.{column}:Q',
alt.Text(f'properties.{column}:Q', format=form)]
).transform_lookup(
lookup='region',
from_=alt.LookupData(choro_data, 'region')
).properties(
width=600,
height=500
)
# Label layer
labels = alt.Chart(choro_data).mark_text(baseline='top'
).properties(
width=600,
height=500
).encode(
longitude='properties.centroid_lon:Q',
latitude='properties.centroid_lat:Q',
text=alt.Text(f'properties.{column}:Q', format=form),
size=alt.value(14),
opacity=alt.value(1)
)
return layer + labels
gen_map(bioenergies_2019, 'Bioenergy in France in 2019', False)
alt.Tooltip
您还可以传递tooltip=[alt.Tooltip('properties.annee:Q', title='Annee'),
alt.Tooltip('properties.region:O', title='Region'),
alt.Tooltip(f'properties.{column}:Q', title=f'{column}')]
参数来指定值的格式;对于数字格式,请使用format
代码;对于日期/时间格式,请使用d3-format代码。