简体   繁体   English

如何使用 altair 突出显示散点图中的标记?

[英]How does one highlight a mark in a scatter plot using altair?

I am trying to replicate the graph below, constructed in seaborn, to altair.我试图复制下面的图表,在 seaborn 中构建,到 altair。 Where I can mark certain points ie predicted point in a cluster.我可以在哪里标记某些点,即集群中的预测点。

在此处输入图片说明

The layer functionality in altair seems to be the direction. Altair 中的图层功能似乎是方向。

Altair Example 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

However I am not able to choose just some of the points to mark with a black dot in the middle.但是我不能只选择一些点来标记中间的黑点。

Thank you谢谢

This can be done by layering two charts containing the data you want to display.这可以通过将包含要显示的数据的两个图表分层来完成。 Here's an example with some data generated by scikit-learn, since you didn't provide any example data:这是 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

在此处输入图片说明

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

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