简体   繁体   English

如何使用 pyplot 为两个数据类创建散点图 plot?

[英]How to create a scatter plot for two data classes with pyplot?

I have two sets of data, x and y as ints.我有两组数据, xy作为整数。 I need to plot both of these data points using matplotlib.pyplot.scatter .我需要使用 plot 这两个数据点matplotlib.pyplot.scatter I also need to plot the first category y == 0 in one color and the second, y == 1 in a different color.我还需要 plot 第一个类别y == 0用一种颜色,第二个类别 y y == 1用另一种颜色。

I've looked at the documentation for the scatter function, but I don't understand how to do all of this in one plot.我查看了散点图 function 的文档,但我不明白如何在一个 plot 中完成所有这些操作。

Sample data:样本数据:

2.897534798034255,0.872359037956732,1
1.234850239781278,-0.293047584301112,1
0.238575209753427,0.129572680572429,0
-0.109757648021958,0.484048547480385,1
1.109735783200013,-0.002785328902198,0
1.572803975652908,0.098547849368397,0

x and y are defined as: x 和 y 定义为:

x = data[:, [0, 1]]
y = data[:, -1].astype(int)

Size of x is 2000, size of y is 1000 x 的大小是 2000,y 的大小是 1000

My attempt:我的尝试:

pl.scatter(x, y==0, s=3, c='r')
pl.scatter(x, y==1, s=3, c='b')
pl.show()

pyplot.scatter() accepts a list of colors, hence: pyplot.scatter()接受 colors 的列表,因此:

c = ['r' if yy==0 else 'b' for yy in y]
plt.scatter(x, y, c=c)

In your code, y==0 produces a mask that has only True and False values, not y values to be plotted.在您的代码中, y==0生成的掩码只有TrueFalse值,而不是要绘制的y值。 If x and y are numpy arrays, you can do:如果xy是 numpy arrays,你可以这样做:

mask = (y == 0)
plt.scatter(x[mask], y[mask], c='r')
mask = (y == 1)
plt.scatter(x[mask], y[mask], c='b')

You can do it like this:你可以这样做:

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[2.897534798034255,0.872359037956732,1],
                 [1.234850239781278,-0.293047584301112,1],
                 [0.238575209753427,0.129572680572429,0],
                 [-0.109757648021958,0.484048547480385,1],
                 [1.109735783200013,-0.002785328902198,0],
                 [1.572803975652908,0.098547849368397,0]])

x = data[:, [0, 1]]
y = data[:, -1].astype(int)

plt.scatter(x[:,0][y==0], x[:,1][y==0], s=3, c='r')
plt.scatter(x[:,0][y==1], x[:,1][y==1], s=3, c='b')
plt.show()

Although this is perhaps more readable:虽然这可能更具可读性:

x1 = data[:, 0]
x2 = data[:, 1]
y = data[:, -1].astype(int)

plt.scatter(x1[y==0], x2[y==0], s=3, c='r')
plt.scatter(x1[y==1], x2[y==1], s=3, c='b')

Output: Output:

散点图

Not sure why you want to extract x and y first and filter later.不知道为什么要先提取xy并稍后过滤。 Given that you have a lot of data and not many categories, plt.plot with markers should also be faster than plt.scatter :鉴于您有大量数据且类别不多,带有标记的plt.plot也应该比plt.scatter更快:

import numpy as np
import matplotlib.pyplot as plt

data = np.asarray([[2.897534798034255,0.872359037956732,1],
                     [1.234850239781278,-0.293047584301112,1],
                     [0.238575209753427,0.129572680572429,0],
                     [-0.109757648021958,0.484048547480385,1],
                     [1.109735783200013,-0.002785328902198,0],
                     [1.572803975652908,0.098547849368397,0]])

colors = ["blue", "red", "green"]
labels = ["A", "B", "C"]

for i, c, l in zip(np.unique(data[:, 2]), colors, labels):   
    plt.plot(data[data[:, 2]==i][:, 0], data[data[:, 2]==i][:, 1], 
             marker="o", markersize=7, ls="None", color=c, 
             label=f"The letter {l} represents category {int(i)}")

plt.legend()
plt.show()

Sample output:样品 output: 在此处输入图像描述

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

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