有没有办法在动态 Altair 图表中保持梯度比例不变?
Is there a way to keep the gradient scale constant in an dynamic Altair chart?
我在 Altair 中有以下代码生成动态叶绿素图:
columns = [str(year) for year in range(20200307, 20200331)]
slider = alt.binding_range(min=20200307, max=20200330, step=1)
select_date = alt.selection_single(name="date_num", fields=['date_num'],
bind=slider, init={'date_num': 20200307})
alt.Chart(states).mark_geoshape(
stroke='black',
strokeWidth=0.05
).project(
type='albersUsa'
).transform_lookup(
lookup='id',
from_=alt.LookupData(data_of_interest, 'id', columns)
).transform_fold(
columns, as_=['date_num', 'case_to_pop_ratio']
).transform_calculate(
date_num='parseInt(datum.date_num)',
case_to_pop_ratio='datum.case_to_pop_ratio'
).encode(
color = alt.condition(
'datum.case_to_pop_ratio > 0',
alt.Color('case_to_pop_ratio:Q', scale=alt.Scale(scheme='lightgreyred')),
alt.value('#dbe9f6')
)
).add_selection(
select_date
).properties(
width=700,
height=450
).transform_filter(
select_date
).resolve_scale(
color='shared'
)
但是,当我在滑块中移动时,生成的图表改变了颜色渐变的比例,使得结果很难解释。有没有办法保持比例一致?
我的数据文件太大 link,但它的格式与此数据文件的格式完全相同,来自单独的 Stack OverFlow 问题:https://github.com/sdasara95/Opioid-Crisis/blob/master/sample_data.csv
您可以通过调整比例域在 Altair 中设置恒定的颜色范围:
import altair as alt
from vega_datasets import data
alt.Chart(data.cars.url).mark_point().encode(
x='Acceleration:Q',
y='Horsepower:Q',
color=alt.Color('Acceleration:Q',scale=alt.Scale(domain=(1, 15))),
)
我在 Altair 中有以下代码生成动态叶绿素图:
columns = [str(year) for year in range(20200307, 20200331)]
slider = alt.binding_range(min=20200307, max=20200330, step=1)
select_date = alt.selection_single(name="date_num", fields=['date_num'],
bind=slider, init={'date_num': 20200307})
alt.Chart(states).mark_geoshape(
stroke='black',
strokeWidth=0.05
).project(
type='albersUsa'
).transform_lookup(
lookup='id',
from_=alt.LookupData(data_of_interest, 'id', columns)
).transform_fold(
columns, as_=['date_num', 'case_to_pop_ratio']
).transform_calculate(
date_num='parseInt(datum.date_num)',
case_to_pop_ratio='datum.case_to_pop_ratio'
).encode(
color = alt.condition(
'datum.case_to_pop_ratio > 0',
alt.Color('case_to_pop_ratio:Q', scale=alt.Scale(scheme='lightgreyred')),
alt.value('#dbe9f6')
)
).add_selection(
select_date
).properties(
width=700,
height=450
).transform_filter(
select_date
).resolve_scale(
color='shared'
)
但是,当我在滑块中移动时,生成的图表改变了颜色渐变的比例,使得结果很难解释。有没有办法保持比例一致?
我的数据文件太大 link,但它的格式与此数据文件的格式完全相同,来自单独的 Stack OverFlow 问题:https://github.com/sdasara95/Opioid-Crisis/blob/master/sample_data.csv
您可以通过调整比例域在 Altair 中设置恒定的颜色范围:
import altair as alt
from vega_datasets import data
alt.Chart(data.cars.url).mark_point().encode(
x='Acceleration:Q',
y='Horsepower:Q',
color=alt.Color('Acceleration:Q',scale=alt.Scale(domain=(1, 15))),
)