我有很多要绘制的图,其中包含很多点。当我尝试使用matplotlib进行操作时,需要花费数小时,这并不方便。存在哪些替代方法?
我的代码的相关位如下,每个特征的点数很容易达到100,000:
marker = 'o'
s = 10
patches = []
import matplotlib.patches as mpatches
for feature, color in zip(features, colors):
for point, value in zip(tsne, df[feature].values):
try:
plt.scatter(point[0], point[1], alpha=value, facecolor=color, marker=marker, s=s, label=feature)
except:
pass
patches.append(mpatches.Rectangle((0, 0), 1, 1, fc=color))
plt.legend(patches, features, prop={'size': 15}, loc='center left', bbox_to_anchor=(1, 0.5))
plt.show();
运行内循环:
for point, value in zip(tsne, df[feature].values):
try:
plt.scatter(point[0], point[1], alpha=value, facecolor=color, marker=marker, s=s, label=feature)
而不是使用一维numpy数组肯定会加快速度。
内部循环可以替换为:
x = tsne[:, 0] # is `tsne` an (n, 2) numpy array?
y = tsne[:, 1]
alpha_values = df[feature].values
try:
plt.scatter(x, y, alpha=alpha_values, facecolor=color, marker=marker, s=s, label=feature)
except:
pass
如果情况对您来说仍然太慢,您也可以切换到datashading in Holoviews,但请先尝试删除内部for循环,因为这肯定会使您放慢速度。