对于大量点,什么比 matplotlib 更快?

What is faster than matplotlib for large numbers of points?

我有一堆情节要制作,其中有大量的点。当我尝试使用 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)

使用 1d 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 循环,因为这肯定会大大降低您的速度。