有没有办法用分箱数据反转 Altair 等值区域图的配色方案?

Tho*_*ker 2 python vega choropleth vega-lite altair

因此,我尝试使用 Altair 中的分箱数据创建县级分区统计图。我可以使用 bin 函数,但无法反转配色方案的方向。如果我使用以下参数,我可以使用非分箱数据更改配色方案的方向:

sort="descending"
Run Code Online (Sandbox Code Playgroud)

使用分箱数据,它不会出现错误,但排序参数不会执行任何操作,这是我一直在使用 vega-lite 画廊示例地图搞乱的完整代码:

import altair as alt
from vega_datasets import data

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

alt.Chart(counties).mark_geoshape().encode(
    color=alt.Color('rate:Q', bin=alt.Bin(maxbins=7), sort="descending", scale=alt.Scale(scheme='yelloworangered'))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).project(
    type='albersUsa'
).properties(
    width=500,
    height=300
)
Run Code Online (Sandbox Code Playgroud)

有没有办法用装箱数据反转配色方案?在这个例子中,我想要的是将失业率较高的县设为黄色,失业率较低的县设为红色。

joe*_*lom 5

您可以通过设置反转 Altair 中的任何比例reverse=True(无需排序)。在你的情况下:

scale=alt.Scale(scheme='yelloworangered', reverse=True)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述