有两种方法可以做到这一点;快速的方法没有记录,将来可能行不通,而更健壮的方法是需要更多的代码。
快速方法依赖于使用 vega 生成的内部字段名称进行分级编码:
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(1701)
X_train = pd.DataFrame({
'Creditworthiness': np.clip(50 + 20 * np.random.randn(300), 0, 100)
})
alt.Chart(X_train).mark_bar().encode(
alt.X('Creditworthiness', bin=True),
y='count()',
color=alt.condition(
alt.datum.bin_maxbins_10_Creditworthiness_end <= 50,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
)
记录的方法是将分箱从编码移至显式转换,这有点冗长:
alt.Chart(X_train).transform_bin(
'Creditworthiness_bin', 'Creditworthiness', bin=alt.Bin(step=10)
).transform_joinaggregate(
count='count()', groupby=['Creditworthiness_bin']
).mark_bar(orient='vertical').encode(
alt.X('Creditworthiness_bin:Q', bin='binned'),
alt.X2('Creditworthiness_bin_end'),
alt.Y('count:Q'),
color=alt.condition(
alt.datum.Creditworthiness_bin_end <= 50,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
)