Matplotlib 自定义转向标记的实时绘制

Matplotlib 自定义转向标记的实时绘制,matplotlib,animation,plot,interactive,Matplotlib,Animation,Plot,Interactive,是否有可能以交互方式绘制自定义标记(如),但让它实时转换?散点图似乎不允许访问标记 您可以创建带有标记的自定义标记。许多样式和选项都是可能的。这样的面片不容易更新,但您可以删除面片并再次创建它以创建动画 创建动画的最简单方法是通过plt.pause(),但这并不适用于所有环境。另一种方法是通过FuncAnimation,这涉及到更多的行,但使控制动画更容易 下面是一些示例代码,以显示这些概念: 导入matplotlib.pyplot作为plt 从matplotlib导入修补程序 从matplot

是否有可能以交互方式绘制自定义标记(如),但让它实时转换?散点图似乎不允许访问标记


您可以创建带有标记的自定义标记。许多样式和选项都是可能的。这样的面片不容易更新,但您可以删除面片并再次创建它以创建动画

创建动画的最简单方法是通过
plt.pause()
,但这并不适用于所有环境。另一种方法是通过
FuncAnimation
,这涉及到更多的行,但使控制动画更容易

下面是一些示例代码,以显示这些概念:

导入matplotlib.pyplot作为plt
从matplotlib导入修补程序
从matplotlib.collections导入PatchCollection
从matplotlib导入动画
将numpy作为np导入
图,ax=plt.子批次()
N=50
x=np.随机均匀(-20,20,(N,2))
dx=np.随机均匀(-1,1,(N,2))
dx/=np.linalg.norm(dx,轴=1,keepdims=True)
颜色=plt.cm.岩浆(np.随机.均匀(0,1,N))
arrow\u style=“简单,头长=2,头宽=3,尾宽=1”
ax.set_xlim(-40,40)
ax.set_ylim(-30,30)
ax.set_方面(“相等”)
旧箭头=无
定义动画(i):
全局旧箭头,x,dx
如果旧箭头不是无:
旧箭头。删除()
x+=dx
dx+=np.随机.均匀(-.1.1,(N,2))
dx/=np.linalg.norm(dx,轴=1,keepdims=True)
arrows=[patches.FancyArrowPatch((xi,yi),(xi+dxi*10,yi+dyi*10),arrowstyle=arrow\u style)
对于zip(x,dx)中的(xi,yi),(dxi,dyi)]
旧箭头=ax.添加集合(PatchCollection(箭头,面颜色=颜色))
返回旧箭头,
ani=动画。FuncAnimation(图,动画,np.arange(1200),
间隔=25,重复=False,blit=True)
plt.show()

您可以创建带有标记的自定义标记。许多样式和选项都是可能的。这样的面片不容易更新,但您可以删除面片并再次创建它以创建动画

创建动画的最简单方法是通过
plt.pause()
,但这并不适用于所有环境。另一种方法是通过
FuncAnimation
,这涉及到更多的行,但使控制动画更容易

下面是一些示例代码,以显示这些概念:

导入matplotlib.pyplot作为plt
从matplotlib导入修补程序
从matplotlib.collections导入PatchCollection
从matplotlib导入动画
将numpy作为np导入
图,ax=plt.子批次()
N=50
x=np.随机均匀(-20,20,(N,2))
dx=np.随机均匀(-1,1,(N,2))
dx/=np.linalg.norm(dx,轴=1,keepdims=True)
颜色=plt.cm.岩浆(np.随机.均匀(0,1,N))
arrow\u style=“简单,头长=2,头宽=3,尾宽=1”
ax.set_xlim(-40,40)
ax.set_ylim(-30,30)
ax.set_方面(“相等”)
旧箭头=无
定义动画(i):
全局旧箭头,x,dx
如果旧箭头不是无:
旧箭头。删除()
x+=dx
dx+=np.随机.均匀(-.1.1,(N,2))
dx/=np.linalg.norm(dx,轴=1,keepdims=True)
arrows=[patches.FancyArrowPatch((xi,yi),(xi+dxi*10,yi+dyi*10),arrowstyle=arrow\u style)
对于zip(x,dx)中的(xi,yi),(dxi,dyi)]
旧箭头=ax.添加集合(PatchCollection(箭头,面颜色=颜色))
返回旧箭头,
ani=动画。FuncAnimation(图,动画,np.arange(1200),
间隔=25,重复=False,blit=True)
plt.show()

我通过
remove()
和如下静态变量解决了这个问题:

class pltMarker:
    def __init__(self, angle=None, pathString=None):
        self.angle = angle or []
        self.pathString = pathString or """simply make and svg, open in a text editor and copy the path XML string in here"""
        self.path = parse_path( self.pathString )
        self.path.vertices -= self.path.vertices.mean( axis=0 )
        self.marker = mpl.markers.MarkerStyle( marker=self.path )
        self.marker._transform = self.marker.get_transform().rotate_deg(angle)

    def rotate(self, angle=0):
        self.marker._transform = self.marker.get_transform().rotate_deg(angle)

def animate(k):

    angle = ... # new angle
    myPltMarker.rotate(angle)

    animate.Scatter.remove()
    animate.Scatter = plt.scatter(1, 0, marker=myPltMarker.marker, s=100)

    return animate.Scatter, 

angle = ...
myPltMarker = pltMarker(angle=angle)
animatePlt.Scatter = plt.scatter(1, 0, marker=myPltMarker.marker, s=100)   

anm = animation.FuncAnimation(fig, animate, blit=False, interval=1)
plt.show()

我通过
remove()
和如下静态变量解决了这个问题:

class pltMarker:
    def __init__(self, angle=None, pathString=None):
        self.angle = angle or []
        self.pathString = pathString or """simply make and svg, open in a text editor and copy the path XML string in here"""
        self.path = parse_path( self.pathString )
        self.path.vertices -= self.path.vertices.mean( axis=0 )
        self.marker = mpl.markers.MarkerStyle( marker=self.path )
        self.marker._transform = self.marker.get_transform().rotate_deg(angle)

    def rotate(self, angle=0):
        self.marker._transform = self.marker.get_transform().rotate_deg(angle)

def animate(k):

    angle = ... # new angle
    myPltMarker.rotate(angle)

    animate.Scatter.remove()
    animate.Scatter = plt.scatter(1, 0, marker=myPltMarker.marker, s=100)

    return animate.Scatter, 

angle = ...
myPltMarker = pltMarker(angle=angle)
animatePlt.Scatter = plt.scatter(1, 0, marker=myPltMarker.marker, s=100)   

anm = animation.FuncAnimation(fig, animate, blit=False, interval=1)
plt.show()

但它甚至没有动画是的,我有。当我在我的交互环境(pycharm)中运行脚本时,它会显示每次启动脚本时都会更改的静态图片,plt.pause会显示动画。我将把代码改为使用FuncAnimation我认为FuncAnimation不适合我。我尝试从integrate获得交互式输出。到目前为止,与MATLAB相比,这是一次可怕的经历。虽然不清楚,但他们的工作方式并没有不同,他们只是没有按照承诺的方式工作或不工作。集成是从scipy开始的,用于解决ODE。我在每次迭代中都会得到新数据,需要用标记更新图形。但它甚至没有动画。是的,我做了。当我在我的交互环境(pycharm)中运行脚本时,它会显示每次启动脚本时都会更改的静态图片,plt.pause会显示动画。我将把代码改为使用FuncAnimation我认为FuncAnimation不适合我。我尝试从integrate获得交互式输出。到目前为止,与MATLAB相比,这是一次可怕的经历。虽然不清楚,但他们的工作方式并没有不同,他们只是没有按照承诺的方式工作或不工作。集成是从scipy开始的,用于解决ODE。我在每次迭代中都会得到新数据,需要使用标记更新图形。