python - Altair:按功能中指定的值旋转文本

标签 python vega vega-lite altair

我想根据 pandas DataFrame 的列中指定的值旋转文本。看来 text_configangle 参数只接受浮点值,没有特征名称。

import pandas as pd
import altair as alt

data = pd.DataFrame(
    {
        "x": [0, 1, 0, -1],
        "y": [1, 0, -1, 0],
        "name": ["UP", "RIGHT", "BOTTOM", "LEFT"],
        "angle": {180, 270, 0, 90},
    }
)

alt.Chart(data).encode(x="x", y="y", text="name").mark_text(angle='angle')

最佳答案

不幸的是,文本角度不能用作编码 channel 。您能做的最好的事情就是以所需的角度手动分割数据和图层标记;例如:

import pandas as pd
import altair as alt

data = pd.DataFrame(
    {
        "x": [0, 1, 0, -1],
        "y": [1, 0, -1, 0],
        "name": ["UP", "RIGHT", "BOTTOM", "LEFT"],
        "angle": [180, 270, 0, 90],
    }
)

base = alt.Chart(data).encode(x="x", y="y", text="name")

layers = [
    base.transform_filter(alt.datum.name == name).mark_text(angle=angle)
    for (name, angle) in zip(data.name, data.angle)
]

alt.layer(*layers)

enter image description here

关于python - Altair:按功能中指定的值旋转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55991996/

相关文章:

python-2.7 - jupyter 笔记本中的 Altair 图表不渲染

charts - 折线图的 Vega-Lite 渐变

python - Pandas:在数据框中创建一个新列,其中的值是根据现有列计算得出的,即。计算最大值

python - 在 urls.py 中使用正则表达式的内联过滤器

python - Pandas 数据框条件 .mean() 取决于特定列中的值

python - 具有不同 x 和 y 编码的 Altair 选择

python - 如何使用 vega-lite 创建与此类似的条形图?

vega-lite - 基于选择动态改变编码中的 Y 轴字段 Vega-Lite

python - Altair slider 变换数据

python - Big Query 如何更改列模式?