简体   繁体   English

使用 alt.condition 标记点角度时出现类型错误

[英]Type error when using alt.condition for mark point angle

I am trying to plot arrows pointing left or right and being green or red, depending on the condition.我正在尝试 plot 箭头指向左或右并且是绿色或红色,具体取决于条件。 It works for the color, but not for the angle of the triangle (mark point) which I'm using for the head of the arrow.它适用于颜色,但不适用于我用于箭头头部的三角形(标记点)的角度。 Here is the data and code:这是数据和代码: 在此处输入图像描述

color=alt.condition("datum.Current >= datum.Previous",alt.value("green"),alt.value("red"))
angle=alt.condition("datum.Current >= datum.Previous",alt.value(210),alt.value(30))
alt.Chart(df_chart).mark_point(size=200,shape='triangle'
                    ,angle=angle).encode(alt.X('Current'),alt.Y('Group'),color=color)

I'm getting this error:我收到此错误: 在此处输入图像描述

This is what I get if I change the angle to a number, it works without error, except that I don't get the red arrow pointing to the left:如果我将角度更改为数字,这就是我得到的结果,它可以正常工作,除了我没有得到指向左侧的红色箭头: 在此处输入图像描述

You can pass alt.condition to the angle encoding rather than the angle mark property:您可以将alt.condition传递给角度编码而不是角度标记属性:

alt.Chart(df).mark_point(size=400, shape='triangle').encode(
    alt.X('Current'),
    alt.Y('Group'),
    angle=alt.condition("datum.Current >= datum.Previous", alt.value(210), alt.value(30)),
    color='Group:N'
)

在此处输入图像描述

You can't use a conditional as for a mark parameter, only for encoding parameters as per https://github.com/altair-viz/altair/issues/1976 .您不能将条件用作mark参数,只能根据https://github.com/altair-viz/altair/issues/1976对参数进行encoding Now, for some reason, it seems like that conditional does not work when passed to the angle encoding either, but you could work around that by using transform_calculate to compute the new field values and reference that field:现在,由于某种原因,似乎条件在传递给angle编码时也不起作用,但您可以通过使用 transform_calculate 来计算新字段值并引用该字段来解决这个问题:

alt.Chart(df_chart).mark_point(size=400, shape='triangle').encode(
    alt.X('Current'),
    alt.Y('Group'),
    angle=alt.Angle('angle:Q', scale=alt.Scale(domain=[0, 360])),
    color='Group:N'
).transform_calculate(
    angle="datum.Current >= datum.Previous ? 210 : 30"
)

在此处输入图像描述

It is important to define the domain, you can see another example here https://altair-viz.github.io/gallery/wind_vector_map.html?highlight=wind .定义域很重要,您可以在此处查看另一个示例https://altair-viz.github.io/gallery/wind_vector_map.ZFC35FDC70D5FC69D269883A822C7A53 Generally I would avoid passing a condition to color just to control the values and instead use the range parameter with an existing field value as explained in the docs https://altair-viz.github.io/user_guide/customization.html#color-domain-and-range一般来说,我会避免将条件传递给颜色来控制值,而是使用具有现有字段值的range参数,如文档https://altair-viz.github.io/user_guide/customization.html#color-域和范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM