本文介绍了在Matplotlib中对3D散点图进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据发布的2d散点图动画在matplotlib中获得散点图的3d动画,",然后在这里上发布3d线图.

I'm trying to get a 3d animation of a scatterplot in matplotlib, based off the 2d scatterplot animation posted here and the 3d line plot posted here.

问题是由于set_dataset_offsets在3D中不起作用而引起的,因此应该使用set_3d_properties附加z信息.玩这个游戏通常会令人窒息,但是运行下面发布的代码.但是,透明度增加得足够多,以至于几帧后这些点才逐渐消失.我在这里做错了什么?我希望这些点在框的边界内跳跃一会儿.即使将步长调整为很小的值也不会降低透明度.

The problems arise from set_data and set_offsets not working in 3D, so you're supposed to use set_3d_properties to tack on the z information. Playing around with that it usually chokes, but with the code posted below it runs. However, the transparency increases enough that the points just fade away after a few frames. What am I doing wrong here? I want the points to jump around within the bounds of the box for a while. Even adjusting the step size to something very small doesn't slow down the transparency.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

FLOOR = -10
CEILING = 10

class AnimatedScatter(object):
    def __init__(self, numpoints=5):
        self.numpoints = numpoints
        self.stream = self.data_stream()
        self.angle = 0

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111,projection = '3d')
        self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
                                           init_func=self.setup_plot, blit=True)

    def change_angle(self):
        self.angle = (self.angle + 1)%360

    def setup_plot(self):
        x, y, z = next(self.stream)
        c = ['b', 'r', 'g', 'y', 'm']
        self.scat = self.ax.scatter(x, y, z,c=c, s=200, animated=True)

        self.ax.set_xlim3d(FLOOR, CEILING)
        self.ax.set_ylim3d(FLOOR, CEILING)
        self.ax.set_zlim3d(FLOOR, CEILING)

        return self.scat,

    def data_stream(self):
        data = np.zeros((3, self.numpoints))
        xyz = data[:3, :]
        while True:
            xyz += 2 * (np.random.random((3, self.numpoints)) - 0.5)
            yield data

    def update(self, i):
        data = next(self.stream)
        data = np.transpose(data)

        self.scat.set_offsets(data[:,:2])
        #self.scat.set_3d_properties(data)
        self.scat.set_3d_properties(data[:,2:],'z')

        self.change_angle()
        self.ax.view_init(30,self.angle)
        plt.draw()
        return self.scat,

    def show(self):
        plt.show()

if __name__ == '__main__':
    a = AnimatedScatter()
    a.show()

推荐答案

终于找到了解决方案,这是不使用触摸颜色来更新点的方法:

Found the solution finally, here is how to update points w/o touching colors:

from mpl_toolkits.mplot3d.art3d import juggle_axes
scat._offsets3d = juggle_axes(xs, ys, zs, 'z')

这是通过set_3d_properties在内部完成的,并重新初始化了颜色

this is internally done by set_3d_properties along with re-initializing colors

这篇关于在Matplotlib中对3D散点图进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:32