bwa*_*en2 0 python vega altair
我正在尝试在等值区域上设置自定义颜色断点,但比例似乎不符合我倾斜的颜色位置。例如:
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
Run Code Online (Sandbox Code Playgroud)
产生:
请注意橙色不是以 0 为中心。如何强制刻度颜色与我的域断点相匹配?
您需要将比例类型设置为 ,"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)
)
Run Code Online (Sandbox Code Playgroud)
在未来的版本中,线性标度类型将成为分段色标的默认值;更多详细信息请访问https://github.com/vega/vega-lite/issues/3980