python - 在 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() #
解决方案
我发现那是我的编码错误。我应该不在循环中而是在循环之外编写 plt.scatter() 函数。由于我绘制了百万次散点图,所以它看起来像是一个无限循环。我犯傻了。
推荐阅读
- servlets - 检查 request.getParameter 是否包含字符串值或整数
- javascript - 未捕获的 TypeError:forEach 不是 javascript 函数中的函数错误
- python - 从浮点数(本机浮点数或 numpy.float64)转换为整数不起作用
- sql - 选择一列中的最大值加上同一表中的其他列
- python - django.db.utils.IntegrityError:NOT NULL 约束失败:home_post.author_id
- python - 计算体素数的 Python 程序
- json.net - NewtonSoft JsonConvert.SerializeObject 间歇性地为每个属性添加前导空格
- sql-server - 将行插入表中的顺序
- javascript - 使用下拉框更改背景 [JavaScript]
- c++ - opencv c++中windows和Linux的结果不同