2016-09-23 25 views
0

Matplotlibを使って3次元プロットを作成し、matplotlibのアニメーションパッケージを作成しようとしています。さらに、アニメーションは、PyQtとQt-Designerを使用して生成されたGuiの一部でなければなりません。現在、私は「animation.Funcnimation()」が正しく、少なくとも私はそう思うの使用についてこだわっている...だからここ は私のコードです:Python:mplot3dとアニメーションを使用してベクトルをアニメーション化する

import sys 
from PyQt4.uic import loadUiType 
from PyQt4 import QtGui 

from matplotlib import pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib import animation 

import numpy as np 
import Quaternion as qt 

Ui_MainWindow, QMainWindow = loadUiType('Newsphere.ui') 

class Kinematic(Ui_MainWindow, QMainWindow): 
    def __init__(self): 
     super(Kinematic, self).__init__() 
     self.setupUi(self) 

     self.fig = plt.figure() 
     self.ax = self.fig.add_subplot(111,projection = '3d') 
     self.fig.tight_layout() 

     self.ani = animation.FuncAnimation(self.fig, self.update, 
             init_func=self.setup_plot, blit=True) 

     self.canvas = FigureCanvas(self.fig) 
     self.mplvl.addWidget(self.canvas) 
     self.canvas.draw() 

    def setup_plot(self): 
     self.ax.view_init(40, 45) 

     self.ax.set_xlabel('X') 
     self.ax.set_ylabel('Y') 
     self.ax.set_zlabel('Z') 

     self.ax.set_xlim3d(-1,1) 
     self.ax.set_ylim3d(-1,1) 
     self.ax.set_zlim3d(-1,1) 

     g_x = np.matrix([[1.0],[0.0],[0.0]]) 
     g_y = np.matrix([[0.0],[1.0],[0.0]]) 
     g_z = np.matrix([[0.0],[0.0],[1.0]]) 

     self.ax.plot([0,g_x[0]], [0,g_x[1]], [0,g_x[2]], label='$X_0$') 
     self.ax.plot([0,g_y[0]], [0,g_y[1]], [0,g_y[2]], label='$Y_0$') 
     self.ax.plot([0,g_z[0]], [0,g_z[1]], [0,g_z[2]], label='$Z_0$') 

     self.vek, = self.ax.plot([0,-1], [0,0], [0,0], label='$g \cdot R$', animated=True) 

     self.ax.legend(loc='best') 
     self.ax.scatter(0,0,0, color='k') 

     return self.vek, 

    def update(self, i): 
     b = self.bslider.value()/100 

     g = np.matrix([[1.0],[0.0],[0.0]]) 
     q = np.array([0,b,0.5,0]) 

     R = qt.QtoR(q) 
     x, y, z = R*g 

     self.vek, = self.ax.plot([0,x], [0,y], [0,z], label='$g \cdot R$', animated=True) #the rotated vector 
     return self.vek, 

if __name__ == '__main__': 

app = QtGui.QApplication(sys.argv) 

main = Kinematic() 
main.show() 

sys.exit(app.exec_()) 

あなただけでそれを実行することはできません"Newsphere.ui"(13行目)とQuaternion.py(11行目)というファイルがないので、コピー貼り付けをしてください。したがって、私が実行すると、次のようになります(実際に私が望むのと同じです): Coordinate system

私の目標は、私が得たデータを使ってベクトル(線50)を描き、それをアニメーション化することです。 Gui-slider(58行目)。誰もこれで私を助けることができますか?私は数日間このことに固執しています!

+2

[最小、完全で、証明可能な例](http://stackoverflow.com/help/mcve) –

+0

を作成してください。私はGUIの.uiファイルを変換する必要があります。 。しかし、私はそれを理解した、私の答えを見てください! – Michael

+0

それがあなたの問題を解決したならば、答えを受け入れることを忘れないでください。 –

答えて

0

もし誰かが上記の問題の解決に興味があるなら、ここで私たちは行く:(これは、 'Newsphere.ui'がないためにコピー貼り付けのコードではないが、重要なスニペット)

import sys 
from PyQt4.uic import loadUiType 
from PyQt4 import QtGui 

from matplotlib import pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib import animation 

import numpy as np 

Ui_MainWindow, QMainWindow = loadUiType('Newsphere.ui') 

class Kinematic(Ui_MainWindow, QMainWindow): 

    def __init__(self): 
     super(Kinematic, self).__init__() 
     self.setupUi(self) 

     self.fig = plt.figure() 
     self.ax = self.fig.add_subplot(111,projection = '3d') 
     self.fig.tight_layout() 
     self.ax.view_init(40, -45) 

     # dashed coordinate system 
     self.ax.plot([0,1], [0,0], [0,0], label='$X_0$', linestyle="dashed") 
     self.ax.plot([0,0], [0,-1], [0,0], label='$Y_0$', linestyle="dashed") 
     self.ax.plot([0,0], [0,0], [0,1], label='$Z_0$', linestyle="dashed") 

     self.ax.set_xlim3d(-1,1) 
     self.ax.set_ylim3d(-1,1) 
     self.ax.set_zlim3d(-1,1) 

     self.ax.set_xlabel('X') 
     self.ax.set_ylabel('Y') 
     self.ax.set_zlabel('Z') 

     self.ax.scatter(0,0,0, color='k') # black origin dot 

     self.canvas = FigureCanvas(self.fig) 
     self.mplvl.addWidget(self.canvas) 

     self.ani = animation.FuncAnimation(self.fig, self.data_gen, init_func=self.setup_plot, blit=True) 

    def setup_plot(self): 

     self.ax.legend(loc='best') 

     self.vek = self.ax.quiver(0, 0, 0, 0, 0, 0, label='$g \cdot R$', pivot="tail", color="black") 

     return self.vek, 

    def data_gen(self, i): 

     b = self.bslider.value()/100 

     vx, vy, vz = np.cos(b), np.sin(b), 0 

     self.vek = self.ax.quiver(0, 0, 0, vx, vy, vz, label='$g \cdot R$', pivot="tail", color="black") 

     self.canvas.draw() 

     return self.vek, 

if __name__ == '__main__': 

    app = QtGui.QApplication(sys.argv) 

    main = Kinematic() 
    main.show() 

    sys.exit(app.exec_()) 

私は

enter image description here

(これらは、同じプロセスを示す1にまとめ二つの絵です)

を取得するコードを実行することにより、 10

this tutorialに続いてNewsphere.uiというGUIファイルを生成しました。基本的にはウィジェット、QSlider、QSpinBoxだけです。ウィジェットにはレイアウト名 "mplvl"があり、41行目に表示されます。これにより、生成されたFigureがウィジェットに追加されます(私はそう思います...)。 QSliderは(QtDesignerで行われた)QSpinBoxに接続され、名前は「bslider」55行目です。この行では、浮動小数点値を生成するスライダが見つかりませんでしたので、スライダ値を100で割っています。私のためのキーラインは61行目でキャンバスが描かれています。現在、animation.FuncAnimation(43行目)は、スライダの値を変更すると新しいベクトルを描画し、写真を比較します。また、変更ベクトルをax.quiverとして描画し、以前の試みのようにax.plotとして描画することは重要です。

改善のためのご質問やご提案がありましたら、お問い合わせください。

2

アニメーション部分に問題があるため、下に回転している矢印をアニメーションするスニペットが表示されます。

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


def data_gen(num): 
    """Data generation""" 
    angle = num * np.pi/36  
    vx, vy, vz = np.cos(angle), np.sin(angle), 1 
    ax.cla() 
    ax.quiver(0, 0, 0, vx, vy, vz, pivot="tail", color="black") 
    ax.quiver(0, 0, 0, vx, vy, 0, pivot="tail", color="black", 
       linestyle="dashed") 
    ax.set_xlim(-1, 1) 
    ax.set_ylim(-1, 1) 
    ax.set_zlim(-1, 1) 
    ax.view_init(elev=30, azim=60) 


fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
data_gen(0) 
ani = animation.FuncAnimation(fig, data_gen, range(72), blit=False) 
plt.show() 

アニメーションに関するドキュメントが最適でない場合があります。しかし、そこにいくつかの例があります。たとえば、Lorenz attractorをアニメーションします。

+0

あなたの返信ありがとう!あなたのスニペットとLorenzアトラクターへのリンクの助けを借りて、私はそれを理解しました。私は別の答えで私の解決策を投稿します... – Michael

関連する問題