牵牛星色阶范围失调

2024-03-29 08:34:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个choropleth上设置自定义的颜色断点,但是刻度似乎不符合我的倾斜颜色放置。例如:

counties = alt.topo_feature(vega_data.us_10m.url, 'counties')

states = alt.topo_feature(vega_data.us_10m.url, 'states')

outlines = alt.Chart(states).mark_geoshape(
    stroke='black'
).project('albersUsa')

domain = [df.min()['rep_vote_change'], 0, df.max()['rep_vote_change']]
range_ = ['darkred', 'orange', 'green']
colors = alt.Chart(counties).mark_geoshape().encode(
    color=alt.Color('rep_vote_change:Q', scale=alt.Scale(domain=domain, range=range_))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(df, 'id', ['rep_vote_change'])
).project(
    type='albersUsa'
).properties(
    width=500,
    height=300
)

colors + outlines 

产生:

enter image description here

注意橙色的中心不是0。如何强制缩放颜色与域断点匹配?在


Tags: dfdata颜色domainrangealtchangefeature
1条回答
网友
1楼 · 发布于 2024-03-29 08:34:07

您需要将scale类型设置为"linear",以使其按预期的方式工作。例如(使用更简单的图表,因为您没有提供数据):

import altair as alt
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'x': np.random.randn(100),
    'y': np.random.randn(100),
    'c': np.random.choice([-10, 0, 1], 100)
})

scale = alt.Scale(
    domain=[-10, 0, 1],
    range=['darkred', 'orange', 'green'],
    type='linear'
)

alt.Chart(df).mark_point().encode(
  x='x',
  y='y',
  color=alt.Color('c', scale=scale)
)

enter image description here

在将来的版本中,线性比例类型将是分段色阶的默认值;有关详细信息,请访问https://github.com/vega/vega-lite/issues/3980

相关问题 更多 >