matplotlib scatterplot传说

发布于 2025-02-09 02:18:15 字数 392 浏览 0 评论 0原文

这是一个常见问题的重复,但是我花了4个小时阅读回复,但我仍然无法理解它们,而我的代码会丢弃错误或返回意外结果。

此任务是如此简单,以至于它应该是一行代码。响应要么是代码墙,要么通过对每个类别进行单独的plt.scatter来执行散点图,我不想这样做。

我如何为此创造传奇? X和Y数据用make_blobs制造。

map = {0: 'red', 1:'green', 2:"blue"}
plt.scatter(X[:,0], X[:,1], c=[map[i] for i in y])

我想要的只是一个看起来像:
的传奇 (红点):0
(绿点):1
(蓝点):2

ffs

This is a repeat of a common question, but I've spent 4 hours reading responses and I still can't make sense of them and my code throws errors or returns unexpected results.

This task is so simple that it should be no more than a single line of code. Responses are either walls of code or they execute the scatter plot by doing a separate plt.scatter call for each class, which I don't want to do.

How do I make a legend for this? X and y data was fabricated with make_blobs.

map = {0: 'red', 1:'green', 2:"blue"}
plt.scatter(X[:,0], X[:,1], c=[map[i] for i in y])

All I want is a legend that looks like:
(red dot): 0
(green dot): 1
(blue dot): 2

ffs

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

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

发布评论

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

评论(1

最偏执的依靠 2025-02-16 02:18:15

由于无法使用完整的代码,我只是创建带有4个条目的虚拟数据,以演示如何使用您正在谈论的3种颜色创建散点图。这将根据您在MAP中输入的内容创建传奇。

import matplotlib.pyplot as plt 
data = {'x1': [1, 3, 6, 9], #Your X values
        'x2':[15, 11, 5, 8], #Your Y values 
        'y': [0,1,2,1]}  #The colors you want for each circle
X=pd.DataFrame(data)

map = {0: 'red', 1:'green', 2:"blue"}
fig, ax = plt.subplots()
for g in X.y.unique():  #For each group R/G/B
    ix = np.where(X.y == g)
    ax.scatter(X.x1[ix[0]], X.x2[ix[0]], c = map[g], label = map[g], s = 50) #Draw with said color
ax.legend()
plt.show()

输出图

”在此处输入图像描述

as the full code is not available, I am just creating dummy data with 4 entries to demonstrate how to create scatter plot with the 3 colors you are talking about. This will create the legend based on what you enter in map.

import matplotlib.pyplot as plt 
data = {'x1': [1, 3, 6, 9], #Your X values
        'x2':[15, 11, 5, 8], #Your Y values 
        'y': [0,1,2,1]}  #The colors you want for each circle
X=pd.DataFrame(data)

map = {0: 'red', 1:'green', 2:"blue"}
fig, ax = plt.subplots()
for g in X.y.unique():  #For each group R/G/B
    ix = np.where(X.y == g)
    ax.scatter(X.x1[ix[0]], X.x2[ix[0]], c = map[g], label = map[g], s = 50) #Draw with said color
ax.legend()
plt.show()

Output graph

enter image description here

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