首页 > 解决方案 > 在 python 中绘制巨大散点图的问题。有一个更好的方法吗?

问题描述

我构建了一个巨大的 ndarray 对象,它是 2059263,2 维(一列用于 x 轴,另一列用于 y 轴)。我尝试使用不同颜色的点绘制散点图。我创建了另一个相同大小的数组列表,其中包含原始 ndarray 的信息。所以我尝试使用matplotlib.pyplot根据信息列表的类别结合ndarray轴信息来绘制散点图。我尝试使用 if 和 elif 条件循环,但循环是无限的......有没有另一种方法来处理一个巨大的轴数组来绘制散点图......?还是我在编码时犯了错误?有没有更快的方法来做到这一点?可能问题是原点轴数组和每个点的信息是分开的......在下面附上我的代码:

import re
import matplotlib.pyplot as plt

cdict = {0 : 'b', 1 : 'c', 2 : 'g', 3 : 'm', 4 : 'r', 5 : 'y',
         6 : 'brown', 7 : 'gold', 8 : 'lightseagreen', 9 : 'indigo', 10 : 'maroon',
         11 : 'cyan', 12 : 'olive', 13 : 'deeppink', 14 : 'sienna', 15 : 'crimson',
         16 : 'peru', 17 : 'lime', 18 : 'navy', 19 : 'orange'}

count = 1
for i in range(len(mapping)):
    if count != int(atr_list[i][1]):
        print("wrong sequence")
        print(count)
        print(atr_list[i])
        break
    else:
        attri = re.search('^\d{3}[0-9]', atr_list[i][0])

        if int(attri.group()) < 2001:
            colo = 0
        elif int(attri.group()) > 2000 and int(attri.group()) < 2006:
            colo = 1
        elif int(attri.group()) > 2005 and int(attri.group()) < 2011:
            colo = 2
        elif int(attri.group()) > 2010 and int(attri.group()) < 2016:
            colo = 3
        elif int(attri.group()) > 2015:
            colo = 4
        plt.scatter(mapping[i, 0], mapping[i, 1], c=cdict[colo])
        count += 1

plt.xlim(mapping[:, 0].min(), mapping[:, 0].max()) #
plt.ylim(mapping[:, 1].min(), mapping[:, 1].max()) #
plt.xlabel('t-SNE_x') #
plt.ylabel('t-SNE_y') #

plt.show() #

标签: pythonmatplotlibscatter-plot

解决方案


我发现那是我的编码错误。我应该不在循环中而是在循环之外编写 plt.scatter() 函数。由于我绘制了百万次散点图,所以它看起来像是一个无限循环。我犯傻了。


推荐阅读