Python 在matplotlib中提供散点图旋转的更快方法?

Python 在matplotlib中提供散点图旋转的更快方法?,python,matplotlib,transform,scatter-plot,Python,Matplotlib,Transform,Scatter Plot,目前,我使用以下方法绘制一组旋转线(地质走向指示器)。然而,即使只有少量的罢工(5000次),这段代码也需要很长时间。每个点都有唯一的旋转。有没有办法给matplotlib一个带有旋转的列表,并比这样一个接一个地旋转更快地执行打印 sample=#3d-array of points(x,y,theta) where theta is an amount I want to rotate the points by. for i in range(len(sample.T)):

目前,我使用以下方法绘制一组旋转线(地质走向指示器)。然而,即使只有少量的罢工(5000次),这段代码也需要很长时间。每个点都有唯一的旋转。有没有办法给matplotlib一个带有旋转的列表,并比这样一个接一个地旋转更快地执行打印

sample=#3d-array of points(x,y,theta) where theta is an amount I want to rotate the points by.

    for i in range(len(sample.T)):
        t = matplotlib.markers.MarkerStyle(marker='|')
        t._transform = t.get_transform().rotate_deg(sample[2,i])
        plt.scatter(sample[0,i],sample[1,i],marker=t,s=50,c='0',linewidth=1)

在这里,您可以创建5000个散点图。这肯定是低效的。您可以使用我在中提出的解决方案,即将各个标记设置为
PathCollection
的路径。这将类似于散点,为标记添加一个额外的参数
m

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.markers as mmarkers

def mscatter(x,y,ax=None, m=None, **kw):
    import matplotlib.markers as mmarkers
    if not ax: ax=plt.gca()
    sc = ax.scatter(x,y,**kw)
    if (m is not None) and (len(m)==len(x)):
        paths = []
        for marker in m:
            if isinstance(marker, mmarkers.MarkerStyle):
                marker_obj = marker
            else:
                marker_obj = mmarkers.MarkerStyle(marker)
            path = marker_obj.get_path().transformed(
                        marker_obj.get_transform())
            paths.append(path)
        sc.set_paths(paths)
    return sc


np.random.seed(42)
data = np.random.rand(5000,3)
data[:,2] *= 360

markers = []
fig, ax = plt.subplots()
for i in range(len(data)):
    t = mmarkers.MarkerStyle(marker='|')
    t._transform = t.get_transform().rotate_deg(data[i,2])
    markers.append(t)
mscatter(data[:,0], data[:,1], m=markers, s=50, c='0', linewidth=1)

plt.show()
如果我们计算这个时间,我们发现这需要大约250毫秒来创建具有5000个点和5000个不同角度的图。相反,循环解决方案需要12秒以上的时间

到目前为止,关于如何旋转多个标记的一般问题。对于这里的特殊情况,似乎需要使用简单的线标记。这可以很容易地用
quiver
绘图来完成。然后可以关闭箭头,使箭头看起来像线

fig, ax = plt.subplots()
ax.quiver(data[:,0], data[:,1], 1,1, angles=data[:,2]+90, scale=1/10, scale_units="dots",
          units="dots", color="k", pivot="mid",width=1, headwidth=1, headlength=0)


结果基本相同,此绘图的优点是只需约80 ms,比
PathCollection
快三倍

你能发布少量的样本数据吗?Beingernest显示的重要曲线图正是我所做的。数据将是三个列表或数组。x位置之一、y位置之一和旋转角度之一。