条件下的 Altair 颜色散点图
Altair Color Scatter Plot on Condition
我有这个 df:
x y term s
0 0.000000 0.132653 matlab 0.893072
1 0.000000 0.142857 matrix 0.905120
2 0.012346 0.153061 laboratory 0.902610
3 0.987654 0.989796 be 0.857932
4 0.938272 0.959184 a 0.861948
变量 s
告诉我们术语与中心线(斜率 1)的“距离”。
我需要制作一个如下所示的散点图:
到目前为止我有这个代码:
chart = alt.Chart(scatterdata_df).mark_circle().encode(
x = alt.X('x:Q', axis = alt.Axis(tickMinStep = 0.05)),
y = alt.Y('y:Q', axis = alt.Axis(tickMinStep = 0.05)),
color=alt.condition('s:Q', alt.value('red'), alt.value('blue')),
tooltip = ['term']
).properties(
width = 500,
height = 500
)
chart
这给了我一个错误。
Javascript Error: Expression parse error: (s:Q)?"red":"blue"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
当我做 color = 's'
时,我得到了这个,它更接近:
但是我又需要那种双渐变的颜色。我知道渐变与 s
变量有关,但我不确定如何使其具有两个渐变,一个用于中心线的每一侧。
s:Q
不是有效的条件语句。但是,例如,您可以编写这样的条件:
color = alt.condition(alt.datum.s < 0, alt.value('red'), alt.value('blue'))
并且带有 s < 0
的点将被标记为红色,所有其他点将被标记为蓝色。
或者,如果您想通过 s
的值对连续色标进行编码(而不是根据条件在两种颜色之间做出决定),您可以这样做
color = 's:Q'
如果您想在这种情况下使用不同于默认的配色方案,您可以这样指定:
color = alt.Color('s:Q', scale=alt.Scale(scheme='redblue'))
其中传递给 scheme
参数的字符串是内置命名配色方案之一,列于 https://vega.github.io/vega/docs/schemes/#reference
有关在 Altair 中自定义颜色的详细信息,请参阅 https://altair-viz.github.io/user_guide/customization.html#customizing-colors
我有这个 df:
x y term s
0 0.000000 0.132653 matlab 0.893072
1 0.000000 0.142857 matrix 0.905120
2 0.012346 0.153061 laboratory 0.902610
3 0.987654 0.989796 be 0.857932
4 0.938272 0.959184 a 0.861948
变量 s
告诉我们术语与中心线(斜率 1)的“距离”。
我需要制作一个如下所示的散点图:
到目前为止我有这个代码:
chart = alt.Chart(scatterdata_df).mark_circle().encode(
x = alt.X('x:Q', axis = alt.Axis(tickMinStep = 0.05)),
y = alt.Y('y:Q', axis = alt.Axis(tickMinStep = 0.05)),
color=alt.condition('s:Q', alt.value('red'), alt.value('blue')),
tooltip = ['term']
).properties(
width = 500,
height = 500
)
chart
这给了我一个错误。
Javascript Error: Expression parse error: (s:Q)?"red":"blue"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
当我做 color = 's'
时,我得到了这个,它更接近:
但是我又需要那种双渐变的颜色。我知道渐变与 s
变量有关,但我不确定如何使其具有两个渐变,一个用于中心线的每一侧。
s:Q
不是有效的条件语句。但是,例如,您可以编写这样的条件:
color = alt.condition(alt.datum.s < 0, alt.value('red'), alt.value('blue'))
并且带有 s < 0
的点将被标记为红色,所有其他点将被标记为蓝色。
或者,如果您想通过 s
的值对连续色标进行编码(而不是根据条件在两种颜色之间做出决定),您可以这样做
color = 's:Q'
如果您想在这种情况下使用不同于默认的配色方案,您可以这样指定:
color = alt.Color('s:Q', scale=alt.Scale(scheme='redblue'))
其中传递给 scheme
参数的字符串是内置命名配色方案之一,列于 https://vega.github.io/vega/docs/schemes/#reference
有关在 Altair 中自定义颜色的详细信息,请参阅 https://altair-viz.github.io/user_guide/customization.html#customizing-colors