简体   繁体   English

使用 matplotlib 绘制包含数千个点及其标签(组)的数据

[英]plotting data with thousands of points and its label(group) with matplotlib

I have an array of 500000 samples ie, the data's shape is (500000, 3) where the first two columns represent x-coordinate and y- coordinate, and the third column is Label values to which the datapoint @ (X,Y) belongs.我有一个包含 500000 个样本的数组,即数据的形状是(500000, 3) ,其中前两列表示 x 坐标和 y 坐标,第三列是数据点 @ (X,Y) 所属的标签值.

for example:- data= [ [20,10, 12.3320], [22, 13, 230.221],.....[..] ]例如:- data= [ [20,10, 12.3320], [22, 13, 230.221],.....[..] ]

I tried the below method.我尝试了以下方法。 But this is too time consuming and poorly interpreted.但这太费时而且解释得不好。

import matplotlib.pyplot as plt

colors = 10*['r.','g.','b.','c.','k.','y.','m.']

for i in range(len(labels)):
    plt.scatter(data[i][0], data[i][1], colors[labels[i]],marker='.')

plt.show()

Is there any other method like imshow() or other which is suitable for the above code which leads to good interpretation?是否有任何其他方法如imshow()或其他适合上述代码的方法可以很好地解释?

The scatter function in matlplotlib is quiet slow, I would recommend to use vispy that use the GPU to plot a large number of points : matlplotlib 中的 scatter 函数很慢,我建议使用使用 GPU 绘制大量点的 vispy :

Works with vispy 0.4.0 that you can install with pip or conda :适用于可以使用 pip 或 conda 安装的 vispy 0.4.0:

pip install vispy

Here is the code (plotted in less than 2sec on my computer):这是代码(在我的电脑上绘制不到 2 秒):

import numpy as np
from vispy import scene, visuals, app
import matplotlib.pyplot as plt

data = np.random.random((500000,3))

canvas = scene.SceneCanvas(keys='interactive', show=True)
view = canvas.central_widget.add_view()
# Create the scatter plot
scatter = scene.visuals.Markers()
scatter.set_data(data[:,:2], face_color=plt.cm.jet(data[:,2]))
view.add(scatter)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
app.run()

there is a nice documentation for vispy and you can customize your plot in the set_data function with arguments like face_color, edge_color, size, edge_width, symbol ... vispy 有一个很好的文档,您可以在set_data函数中使用face_color、edge_color、size、edge_width、symbol ...等参数自定义绘图

Good luck with your data visualization ;)祝你的数据可视化好运 ;)

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

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