连续3D绘图使用python-matplotlib(即图更新)? [英] Continuous 3D plotting (i.e. figure update) using python-matplotlib?

查看:2283
本文介绍了连续3D绘图使用python-matplotlib(即图更新)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模拟,其计算表面数据的模拟的每次迭代。 我想继续绘制该数据作为表面图同一个窗口(更新在每次迭代中的情节),以便看看它是如何演变和检查的算法。

我的想法是创建一个类,将初始化窗口/剧情,然后从模拟循环内重绘该窗口。这里是我想出了一个类:

 进口numpy的为NP
进口matplotlib.pyplot为PLT
从mpl_toolkits.mplot3d进口Axes3D
从matplotlib进口厘米
从matplotlib.ticker进口LinearLocator,FixedLocator,FormatStrFormatter
进口matplotlib
matplotlib.interactive(假)

类plot3dClass(对象):

    高清__init __(个体经营,systemSideLength,lowerCutoffLength):
        self.systemSideLength = systemSideLength
        self.lowerCutoffLength = lowerCutoffLength
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111,投影='3D')
        self.ax.set_zlim3d(-10E-9,10E9)

        X = np.arange(0,self.systemSideLength,self.lowerCutoffLength)
        Y = X
        self.X,self.Y = np.meshgrid(X,Y)

        self.ax.w_zaxis.set_major_locator(LinearLocator(10))
        self.ax.w_zaxis.set_major_formatter(FormatStrFormatter(%.03f'))

        heightR = np.zeros(self.X.shape)
        self.surf = self.ax.plot_surface(self.X,self.Y,heightR,rstride = 1,cstride = 1,CMAP = cm.jet,线宽= 0,抗锯齿= FALSE)
        #〜self.fig.colorbar(self.surf,收缩= 0.5,纵横= 5)

        plt.show()


    高清的DrawNow(个体经营,heightR):

        self.surf = self.ax.plot_surface(self.X,self.Y,heightR,rstride = 1,cstride = 1,CMAP = cm.jet,线宽= 0,抗锯齿= FALSE)
        plt.draw()#重绘帆布

        time.sleep(1)
 

这个问题我有这个code,就是code停在plt.show(),只有继续下去,当我关闭情节窗口。另外,我不知道的叫声self.ax.plot_surface(...)'和'plt.draw()将更新的数字,因为我想它。

那么,这个类正确的方向?

如果是:?需要什么样的修改

如果不是?可能有人请给我建议如何实现我想要的

我意识到,这个问题似乎微不足道的人,但我(老实说)确实花了整整一天昨日在谷歌和努力,我很茫然......

任何帮助将大大AP preciated,让我可以回到我的实际工作。

坦克很多提前。

作为参考:

我还发现以下code这不,我想要的,但它是在2D,所以它不会帮我直接:

 从pylab进口*
进口时间

离子()

TSTART = time.time()#建档
X = arange(0.2 * PI,0.01)#的X阵列
行,积=(X,SIN(X))

因为我在arange(1200):
    line.set_ydata(SIN(X + I / 10.0))#更新数据
    画()#重绘帆布

打印FPS:,200 /(time.time() -  TSTART)
 

解决方案

您不必 plt.show(),如果它是一个动画(互动)的情节。你也想​​的互动设置为true,没有虚假记载,这是与调用离子()在2D的例子。此外,您还需要删除()从previous框架的表面图,如果你不希望看到他们所有。

否则你是pretty的接近。

这对我的作品:

 进口numpy的为NP
进口matplotlib.pyplot为PLT
从mpl_toolkits.mplot3d进口Axes3D
从matplotlib进口厘米
从matplotlib.ticker进口LinearLocator,FixedLocator,FormatStrFormatter
进口matplotlib,时间

类plot3dClass(对象):

    高清__init __(个体经营,systemSideLength,lowerCutoffLength):
        self.systemSideLength = systemSideLength
        self.lowerCutoffLength = lowerCutoffLength
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111,投影='3D')
        self.ax.set_zlim3d(-10E-9,10E9)

        RNG = np.arange(0,self.systemSideLength,self.lowerCutoffLength)
        self.X,self.Y = np.meshgrid(RNG,RNG)

        self.ax.w_zaxis.set_major_locator(LinearLocator(10))
        self.ax.w_zaxis.set_major_formatter(FormatStrFormatter(%.03f'))

        heightR = np.zeros(self.X.shape)
        self.surf = self.ax.plot_surface(
            self.X,self.Y,heightR,rstride = 1,cstride = 1,
            CMAP = cm.jet,线宽= 0,抗锯齿= FALSE)
        #plt.draw()也许你想看到这架?

    高清的DrawNow(个体经营,heightR):
        self.surf.remove()
        self.surf = self.ax.plot_surface(
            self.X,self.Y,heightR,rstride = 1,cstride = 1,
            CMAP = cm.jet,线宽= 0,抗锯齿= FALSE)
        plt.draw()#重绘帆布
        time.sleep(1)

matplotlib.interactive(真)

p值= plot3dClass(5,1)
因为我在范围内(2):
    p.drawNow(np.random.random(p.X.shape))
 

I have a simulation which calculates surface data for each iteration of the simulation. I would like to continuously plot that data as a surface plot to the same window (updating the plot in each iteration) in order to see how it evolves and to check the algorithm.

My Idea was to create a class that would initialize the window/plot and then redraw to that window from inside the simulation loop. Here is the class I came up with:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib
matplotlib.interactive( False )

class plot3dClass( object ):

    def __init__( self, systemSideLength, lowerCutoffLength ):
        self.systemSideLength = systemSideLength
        self.lowerCutoffLength = lowerCutoffLength
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot( 111, projection='3d' )
        self.ax.set_zlim3d( -10e-9, 10e9 )

        X = np.arange( 0, self.systemSideLength, self.lowerCutoffLength )
        Y = X
        self.X, self.Y = np.meshgrid(X, Y)

        self.ax.w_zaxis.set_major_locator( LinearLocator( 10 ) )
        self.ax.w_zaxis.set_major_formatter( FormatStrFormatter( '%.03f' ) )

        heightR = np.zeros( self.X.shape )
        self.surf = self.ax.plot_surface( self.X, self.Y, heightR, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False )
        #~ self.fig.colorbar( self.surf, shrink=0.5, aspect=5 )

        plt.show()


    def drawNow( self, heightR ):

        self.surf = self.ax.plot_surface( self.X, self.Y, heightR, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False )
        plt.draw()                      # redraw the canvas

        time.sleep(1)

The problem I have with this code, is that the code stops at the 'plt.show()' and only continues, when I close the plot-window. Also I am not sure if the calls of 'self.ax.plot_surface( ... )' and 'plt.draw()' would update the figure as I would like it.

So is this class the right direction?

If yes: What modifications are needed?

If not: Could somebody please give me advice how to achieve what I want?

I realize that this problem might seem trivial to others, but I (honestly) did spend the whole day yesterday on Google and trying and I'm at a loss...

Any help would greatly appreciated, so that I can get back to my actual work.

Tanks alot in advance.

As a reference:

I also found the following code which does, what I want, but it is in 2D, so it does not help me directly:

from pylab import *
import time

ion()

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))

for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas

print 'FPS:' , 200/(time.time()-tstart)

解决方案

You do not need to plt.show() if it is an animated (interactive) plot. You also want interactive set to True, not False which is the same as calling ion() in your 2d example. Also, you need to remove() the surface plots from previous frames if you do not want to see them all.

Otherwise you were pretty close.

This works for me:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib, time

class plot3dClass( object ):

    def __init__( self, systemSideLength, lowerCutoffLength ):
        self.systemSideLength = systemSideLength
        self.lowerCutoffLength = lowerCutoffLength
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot( 111, projection='3d' )
        self.ax.set_zlim3d( -10e-9, 10e9 )

        rng = np.arange( 0, self.systemSideLength, self.lowerCutoffLength )
        self.X, self.Y = np.meshgrid(rng,rng)

        self.ax.w_zaxis.set_major_locator( LinearLocator( 10 ) )
        self.ax.w_zaxis.set_major_formatter( FormatStrFormatter( '%.03f' ) )

        heightR = np.zeros( self.X.shape )
        self.surf = self.ax.plot_surface( 
            self.X, self.Y, heightR, rstride=1, cstride=1, 
            cmap=cm.jet, linewidth=0, antialiased=False )
        # plt.draw() maybe you want to see this frame?

    def drawNow( self, heightR ):
        self.surf.remove()
        self.surf = self.ax.plot_surface( 
            self.X, self.Y, heightR, rstride=1, cstride=1, 
            cmap=cm.jet, linewidth=0, antialiased=False )
        plt.draw()                      # redraw the canvas
        time.sleep(1)

matplotlib.interactive(True)

p = plot3dClass(5,1)
for i in range(2):
    p.drawNow(np.random.random(p.X.shape))

这篇关于连续3D绘图使用python-matplotlib(即图更新)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆