如何使用 altair 在散点图中突出显示标记?
How does one highlight a mark in a scatter plot using altair?
我正在尝试将在 seaborn 中构建的下图复制到 altair。我可以在哪里标记某些点,即集群中的预测点。
Altair 中的层功能似乎是方向。
Altair 示例
import altair as alt
import pandas as pd
source = pd.DataFrame({
'x': [1, 3, 5, 7, 9],
'y': [1, 3, 5, 7, 9],
'label': ['A', 'B', 'C', 'D', 'E']
})
bars = alt.Chart(source).mark_point().encode(
x='x:Q',
y='y:Q'
)
text = bars.mark_text(
align='left',
baseline='middle',
dx=7
).encode(
text='label'
)
bars + text
但是我不能只选择一些点在中间用黑点标记。
谢谢
这可以通过将包含您要显示的数据的两个图表分层来实现。这是一个由 scikit-learn 生成的一些数据的示例,因为您没有提供任何示例数据:
import altair as alt
import pandas as pd
from sklearn.datasets import make_blobs
X, labels = make_blobs(20, random_state=1)
points = pd.DataFrame({
'x': X[:, 0],
'y': X[:, 1],
'labels': labels
})
centers = points.groupby('labels').mean()
data = pd.concat([points , centers.reset_index()])
chart1 = alt.Chart(data).mark_point(filled=True, size=150).encode(
x='x',
y='y',
color='labels:N'
)
chart2 = alt.Chart(centers).mark_point(filled=True, size=50).encode(
x='x',
y='y',
color=alt.value('black')
)
chart1 + chart2
我正在尝试将在 seaborn 中构建的下图复制到 altair。我可以在哪里标记某些点,即集群中的预测点。
Altair 中的层功能似乎是方向。
Altair 示例
import altair as alt
import pandas as pd
source = pd.DataFrame({
'x': [1, 3, 5, 7, 9],
'y': [1, 3, 5, 7, 9],
'label': ['A', 'B', 'C', 'D', 'E']
})
bars = alt.Chart(source).mark_point().encode(
x='x:Q',
y='y:Q'
)
text = bars.mark_text(
align='left',
baseline='middle',
dx=7
).encode(
text='label'
)
bars + text
但是我不能只选择一些点在中间用黑点标记。
谢谢
这可以通过将包含您要显示的数据的两个图表分层来实现。这是一个由 scikit-learn 生成的一些数据的示例,因为您没有提供任何示例数据:
import altair as alt
import pandas as pd
from sklearn.datasets import make_blobs
X, labels = make_blobs(20, random_state=1)
points = pd.DataFrame({
'x': X[:, 0],
'y': X[:, 1],
'labels': labels
})
centers = points.groupby('labels').mean()
data = pd.concat([points , centers.reset_index()])
chart1 = alt.Chart(data).mark_point(filled=True, size=150).encode(
x='x',
y='y',
color='labels:N'
)
chart2 = alt.Chart(centers).mark_point(filled=True, size=50).encode(
x='x',
y='y',
color=alt.value('black')
)
chart1 + chart2