机器学习可视化散点图

0 投票
1 回答
51 浏览
提问于 2025-04-14 16:54

我正在努力复现提供的图片中的可视化风格。看起来是用散点图来展示数据点。当我尝试创建类似的可视化时,所有的位置都变得可见。我的目标是创建另一个散点图,里面的圆圈要更大,活动较多的地方用深红色表示,而活动较少的地方用浅红色表示。

我觉得我需要设置一个阈值,来选择性地显示数据点,而不是把所有的数据点都显示出来。有没有人能给我一些建议,告诉我如何实现类似于图片中那样的可视化?

这是我现在的图:

这里输入图片描述

这是我想要的图:

我想要的散点图

这是我的代码:


def plot_cumulative_heatmap(positions: List[Tuple[float, float]], map_name: str = "de_mirage", map_type: str = "original", dark: bool = False, titel: str = "Counter-Terrorist Attacking", isPistol: str = "Pistol Round") -> Literal[True]:
    heatmap_fig, heatmap_axes = plot_map(map_name=map_name, map_type=map_type, dark=dark)
    
    heatmap_data = []

    for position in positions:
        x, y = position
        heatmap_data.append((x, y))

    # Extract data for plotting
    x, y = zip(*heatmap_data)

    sns.scatterplot(x=x, y=y, color='red', s=5, ax=heatmap_axes, alpha=0.5)
    heatmap_axes.set_title(f"{titel}\nTotal Positions: {len(positions)}\n{isPistol}", fontsize=14, weight="bold")
    heatmap_axes.get_xaxis().set_visible(False)
    heatmap_axes.get_yaxis().set_visible(False)

    print("Heatmap saved.")
    return True

1 个回答

1

每个输入点一个透明圆圈

下面的方法为每个输入的坐标点画上大大的透明圆圈。如果输入中有多个点坐标是一样的,它们的透明度会叠加在一起。

import matplotlib.pyplot as plt
import numpy as np

# create some dummy test data
np.random.seed(20240311)
x = np.random.randn(20, 25).cumsum(axis=1).ravel()
y = np.random.randn(20, 25).cumsum(axis=1).ravel()
filter = (np.abs(x) < 7) & (np.abs(y) < 7) & ((np.abs(x) > 5) | (np.abs(y) > 5))
x = x[filter]
y = y[filter]

fig, ax = plt.subplots()
fig.set_facecolor('black')
ax.set_aspect('equal', 'datalim')
ax.scatter(x, y, color='salmon', alpha=0.3, lw=0, s=500)

# draw rectangles representing the walls
ax.add_patch(plt.Rectangle((-7, -7), 14, 14, fc='none', ec='w', lw=2))
ax.add_patch(plt.Rectangle((-5, -5), 10, 10, fc='none', ec='w', lw=2))
ax.axis('off')

plt.show()

为散点绘制透明圆圈

使用KMeans将点分组,透明度根据数量变化

另一种方法是使用KMeans算法来找出靠得很近的点的中心。然后,可以在这些中心位置画上大点,透明度根据每个组里点的数量来决定。

你可以改变组的数量(这里是n_clusters=20),这样就能得到更多或更少的圆圈。

from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np

# dummy test data from the other example

kmeans = KMeans(n_clusters=20, init='k-means++', random_state=0)
kmeans.fit(np.c_[x, y])

centroids = kmeans.cluster_centers_
labels = kmeans.labels_
counts = np.bincount(labels)
max_count = counts.max()

fig, ax = plt.subplots()
fig.set_facecolor('black')
ax.set_facecolor('none')
alphas = 0.2 + 0.8 * counts / max_count
ax.scatter(x=centroids[:, 0], y=centroids[:, 1], color='salmon', s=1000, lw=0, alpha=alphas)
ax.set_aspect('equal', 'datalim')

# draw rectangles representing the walls
ax.add_patch(plt.Rectangle((-7, -7), 14, 14, fc='none', ec='w', lw=2, zorder=0))
ax.add_patch(plt.Rectangle((-5, -5), 10, 10, fc='none', ec='w', lw=2, zorder=0))
ax. Axis('off')
plt.tight_layout()
plt.show()

KMeans找出聚类中心,透明度根据数量

撰写回答