Altair:按功能中指定的值旋转文本
Altair: rotate text by value specified in feature
我想根据 pandas DataFrame 的列中指定的值旋转文本。 text_config
的 angle
参数似乎只接受浮点值,没有特征名称。
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')
很遗憾,文本角度不能用作编码通道。您能做的最好的事情就是以所需的角度手动拆分数据和图层标记;例如:
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)
我想根据 pandas DataFrame 的列中指定的值旋转文本。 text_config
的 angle
参数似乎只接受浮点值,没有特征名称。
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')
很遗憾,文本角度不能用作编码通道。您能做的最好的事情就是以所需的角度手动拆分数据和图层标记;例如:
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)