Altair 颜色分级值
Altair Color binned values
无法为以下直方图中的合并值着色。我打算为 x 轴(信誉)上小于 50 的所有条着色。这是如何在 Altair 中完成的?
base = alt.Chart(X_train)
histogram = base.mark_bar().encode(
alt.X('Creditworthiness', bin=True),
y='count()',
color=alt.condition(
alt.datum.Creditworthiness < 50,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
)
threshold_line = pd.DataFrame([{"threshold": max_profit_threshold}])
mark = alt.Chart(threshold_line).mark_rule(color="#e45755").encode(
x='threshold:Q',
size=alt.value(2)
)
histogram + mark
有两种方法可以做到这一点;未记录且将来可能无法使用的快速方法,以及更多代码的更健壮的方法。
快速方法依赖于使用 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
)
)
这是一种使用已记录的 Altair 参数的既不冗长又可靠的方法。这是通过合并颜色编码(使用给定阈值作为 bin 步长),同时限制色标(颜色取自“category10”分类配色方案)来实现的。
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)
})
threshold = 50
alt.Chart(X_train).mark_bar().encode(
alt.X('Creditworthiness', bin=True),
y='count()',
color=alt.Color('Creditworthiness', bin=alt.Bin(step=threshold), scale=alt.Scale(domain=[0, threshold], range=["#1f77b4", "#ff7f0e"]), legend=None),
)
无法为以下直方图中的合并值着色。我打算为 x 轴(信誉)上小于 50 的所有条着色。这是如何在 Altair 中完成的?
base = alt.Chart(X_train)
histogram = base.mark_bar().encode(
alt.X('Creditworthiness', bin=True),
y='count()',
color=alt.condition(
alt.datum.Creditworthiness < 50,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
)
threshold_line = pd.DataFrame([{"threshold": max_profit_threshold}])
mark = alt.Chart(threshold_line).mark_rule(color="#e45755").encode(
x='threshold:Q',
size=alt.value(2)
)
histogram + mark
有两种方法可以做到这一点;未记录且将来可能无法使用的快速方法,以及更多代码的更健壮的方法。
快速方法依赖于使用 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
)
)
这是一种使用已记录的 Altair 参数的既不冗长又可靠的方法。这是通过合并颜色编码(使用给定阈值作为 bin 步长),同时限制色标(颜色取自“category10”分类配色方案)来实现的。
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)
})
threshold = 50
alt.Chart(X_train).mark_bar().encode(
alt.X('Creditworthiness', bin=True),
y='count()',
color=alt.Color('Creditworthiness', bin=alt.Bin(step=threshold), scale=alt.Scale(domain=[0, threshold], range=["#1f77b4", "#ff7f0e"]), legend=None),
)