python - 格式化 Altair 等值线图色标

标签 python altair

我正在尝试在等值线图上使用色标对百分比进行编码。我如何设置刻度的格式以显示百分比值(例如 10%)而不是分数(例如 0.1)?我知道对于常规的 X 或 Y 轴,您可以执行 axis=alt.Axis(format='.0%'),但我看不到如何使用此色标的格式。

import altair as alt
import pandas as pd
from vega_datasets import data
alt.renderers.enable('notebook')

airports = pd.read_csv(data.airports.url)
airports = airports.groupby('state').agg({'iata': 'count'})
airports['id'] = range(1, len(airports) + 1)
airports['pct'] = airports['iata'] / airports['iata'].sum()

states = alt.topo_feature(data.us_10m.url, feature='states')

alt.Chart(states).mark_geoshape().encode(
    color='pct:Q'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(airports, 'id', ['pct'])
).project('albersUsa')

enter image description here

最佳答案

颜色编码的 legend 属性接受控制图例比例格式的 format 参数:

alt.Chart(states).mark_geoshape().encode(
    color=alt.Color('pct:Q', legend=alt.Legend(format=".0%"))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(airports, 'id', ['pct'])
).project('albersUsa')

enter image description here

更多信息在 alt.Legend documentation .

关于python - 格式化 Altair 等值线图色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58400759/

相关文章:

python - 自定义交互式图表中的图例和色标 `altair`

python - Altair 条形图 - 标签放置和格式

python - 使 Altair 散点图分配不同的颜色而不是相同颜色的阴影

python 牵牛星 : How to save figures in higher resolution

python - 对最后一个元素使用一种形状,对图表中的所有其他元素使用另一种形状

java - 是否必须了解机器体系结构才能编写代码?

python - 异常中止后重新启动python脚本时,Mysql python代码出现问题

python - 属性错误 : 'NoneType' object has no attribute 'route' and webapp2

python - 装饰器会消耗更多内存吗?

python - Python 中的变量 : Add and change variables