如何使中心的颜色使用相同的簇颜色?

发布于 2025-01-23 18:09:57 字数 1407 浏览 3 评论 0原文

我有两种方法用于绘制群集的中心

def draw_roh_delta(self, rho, delta, center, plot):
        plot.scatter(rho, delta, label='rho-delta', c='k', s=5)
        plot.set_xlabel('rho')
        plot.set_ylabel('delta')
        center_rho = rho[center]
        center_delta = delta[center]
        np.random.seed(6)
        colors = np.random.rand(len(center), 3)
        plot.scatter(center_rho, center_delta, c=colors)
        plot.legend()

,还有其他用于绘制群集

def draw_cluster(self, title, cluster, halo, points, plot):
    cluster_points = dict()
    colors = dict()
    np.random.seed(10)
    for k, v in cluster.items():
        cluster_points[k] = points.loc[cluster[k], :]
        colors[k] = np.random.rand(3)
    for k, v in cluster_points.items():
        plot.scatter(v.loc[:, 'x'], v.loc[:, 'y'], c=colors[k], alpha=0.5)
        plot.scatter(v.at[k, 'x'], v.at[k, 'y'], c=colors[k], s=np.pi * 10 ** 2)

的方法,我需要使中心的颜色与簇相同,而不是随机创建它们。有人可以帮忙吗?

编辑

def draw_roh_delta(self, rho, delta, center, plot):
    plot.scatter(rho, delta, label='rho-delta', c='k', s=5)
    plot.set_xlabel('rho')
    plot.set_ylabel('delta')
    center_rho = rho[center]
    center_delta = delta[center]
    color= ['red', 'blue', 'green', 'yellow','orange','purple']
    plot.scatter(center_rho, center_delta, c=color)
    plot.legend()

i have two methods one for drawing the centers for the cluster

def draw_roh_delta(self, rho, delta, center, plot):
        plot.scatter(rho, delta, label='rho-delta', c='k', s=5)
        plot.set_xlabel('rho')
        plot.set_ylabel('delta')
        center_rho = rho[center]
        center_delta = delta[center]
        np.random.seed(6)
        colors = np.random.rand(len(center), 3)
        plot.scatter(center_rho, center_delta, c=colors)
        plot.legend()

and others for drawing the cluster

def draw_cluster(self, title, cluster, halo, points, plot):
    cluster_points = dict()
    colors = dict()
    np.random.seed(10)
    for k, v in cluster.items():
        cluster_points[k] = points.loc[cluster[k], :]
        colors[k] = np.random.rand(3)
    for k, v in cluster_points.items():
        plot.scatter(v.loc[:, 'x'], v.loc[:, 'y'], c=colors[k], alpha=0.5)
        plot.scatter(v.at[k, 'x'], v.at[k, 'y'], c=colors[k], s=np.pi * 10 ** 2)

I need to make the color of the centers the same color as the clusters instead of creating them randomly. can someone help, please?

EDIT

def draw_roh_delta(self, rho, delta, center, plot):
    plot.scatter(rho, delta, label='rho-delta', c='k', s=5)
    plot.set_xlabel('rho')
    plot.set_ylabel('delta')
    center_rho = rho[center]
    center_delta = delta[center]
    color= ['red', 'blue', 'green', 'yellow','orange','purple']
    plot.scatter(center_rho, center_delta, c=color)
    plot.legend()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2025-01-30 18:09:57

一种方法是使用绘图函数来绘制点,该函数返回list [line2d]。您只需要取出第一个元素并调用其get_color()方法即可获得这次绘制的颜色。

我从Kmeans聚类中提取了我的结果:

fig, ax = plt.subplots()

for i, center in enumerate(centers):
    color = ax.plot(*center, '*')[0].get_color()
    points = data_set[labels == i]
    ax.plot(*points.T, 'o', c=color)

不幸的是,要做到这一点,您可能需要将这两种方法融合在一起,或者让第一种方法将所有颜色返回第二种方法。

edit

我会这样解决:

def draw_rho_delta(self, rho, delta, center, plot):
    # ...
    self.colors = []
    for c_rho, c_delta in zip(center_rho, center_delta):
        color = plot.plot(c_rho, c_delta, 'o')[0].get_color()
        self.colors.append(color)

def draw_cluster(self, title, cluster, halo, points, plot):
    # ...
    for (k, v), color in zip(cluster.items(), self.colors):
        p = points.loc[v, :]
        plot.plot(p.loc[:, 'x'], p.loc[:, 'y'], 'o', c=color)
        plot.plot(p.at[k, 'x'], p.at[k, 'y'], 'o', c=color)

我不知道您的plot的类型,所以我假设它是matplotlib.pyplot.axes.axes,而且我认为您绘制中心的顺序与绘制簇的顺序一致。

One way is to use the plot function to draw points, which returns a list[Line2d]. You just need to take out the first element and call its get_color() method obtains the color drawn this time.

I drew my result of kmeans clustering as follow:

fig, ax = plt.subplots()

for i, center in enumerate(centers):
    color = ax.plot(*center, '*')[0].get_color()
    points = data_set[labels == i]
    ax.plot(*points.T, 'o', c=color)

Unfortunately, to do this, you may need to blend the two methods together, or let the first method return all colors to the second method.

EDIT

I would solve it like this:

def draw_rho_delta(self, rho, delta, center, plot):
    # ...
    self.colors = []
    for c_rho, c_delta in zip(center_rho, center_delta):
        color = plot.plot(c_rho, c_delta, 'o')[0].get_color()
        self.colors.append(color)

def draw_cluster(self, title, cluster, halo, points, plot):
    # ...
    for (k, v), color in zip(cluster.items(), self.colors):
        p = points.loc[v, :]
        plot.plot(p.loc[:, 'x'], p.loc[:, 'y'], 'o', c=color)
        plot.plot(p.at[k, 'x'], p.at[k, 'y'], 'o', c=color)

I don't know the type of your plot, so I assume it's matplotlib.pyplot.Axes, and I assume that the order in which you draw the centers is consistent with the order in which you draw the clusters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文