使用Python动画3D条形图

时间:2016-02-15 17:40:04

标签: python matplotlib

我必须在3d条形图中显示一些数据 z = f(x,y,t)。我想随着时间的推移改变/动画这个图表。

现在,我可以在任何给定时间 t 显示我的数据 z = f(x,y,t),但我找不到方法自动重绘图表以显示下一个时间步的数据。有没有办法做到这一点?

我尝试了一个简单的循环,但显然我只能看到最后一步的数据。

以下是我的代码的当前版本:

from mpl_toolkits.mplot3d import Axes3D
from math import cos, sin
import matplotlib.pyplot as plt 
import numpy as np

# An arbitraty function for z = f(x,t)
def z_xt(x, t):
    return 30*sin(5*t) + x**2 + 20

# An arbitraty function for z = f(y,t)
def z_yt(y, t):
    return 20*cos(2*t) + y**3/10 + 20

# Superposition z(x,y,t) = z(x,t) + z(y,t)
def z_xyt(f_xt, f_yt):
    return f_xt+f_yt

# Definition of space and time domains
nx = 9;  dx = 1
ny = 6;  dy = 1
nt = 10; dt = 1
y, x   = np.mgrid[slice(0, ny, dy),slice(0, nx, dx)]
t_list = [round(t*dt,2) for t in range(nt)]

# The matrix that contains the solution for every time step
Z_xyt  = [z_xyt(z_xt(x, t), z_yt(y, t)) for t in t_list]

# Plot data in a 3D bar chart
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_grid, y_grid = np.meshgrid(np.arange(nx), np.arange(ny))
x_grid = x_grid.flatten()
y_grid = y_grid.flatten()

# Iterate time and plot coresponding data
for index, t in enumerate(t_list):    
    # Z_xyt data at time t_list[index] 
    z_data = Z_xyt[index].flatten()

    # Draw/actualize 3D bar chart data for every time step                                     
    bar_chart = ax.bar3d(x_grid, y_grid, np.zeros(len(z_data)), 1, 1, z_data)
    plt.draw()

plt.show()

提前致谢!

1 个答案:

答案 0 :(得分:1)

我终于通过在PyQt窗口中嵌入图表找到了一种方法(按照in this post给出的说明)。下面提出的代码生成一个PyQt窗口,其中显示了3D条形图。可以使用滑块(时间变化)对图表进行动画处理。

import sys
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from mpl_toolkits.mplot3d import Axes3D
from math import cos, sin
import matplotlib.pyplot as plt 
import numpy as np

# An arbitraty function for z = f(x,t)
def z_xt(x, t):
    return 30*sin(5*t) + x**2 + 20

# An arbitraty function for z = f(y,t)
def z_yt(y, t):
    return 20*cos(2*t) + y**3/10 + 20

# Superposition z(x,y,t) = z(x,t) + z(y,t)
def z_xyt(f_xt, f_yt):
    return f_xt+f_yt

# Definition of space and time domains
nx = 9;  dx = 1
ny = 6;  dy = 1
nt = 50; dt = 0.05
y, x   = np.mgrid[slice(0, ny, dy),slice(0, nx, dx)]
t_list = [round(t*dt,2) for t in range(nt)]

# The matrix that contains the solution for every time step
Z_xyt  = [z_xyt(z_xt(x, t), z_yt(y, t)) for t in t_list]

# Plot data in a 3D bar chart
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_grid, y_grid = np.meshgrid(np.arange(nx), np.arange(ny))
x_grid = x_grid.flatten()
y_grid = y_grid.flatten()


class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # A slider to make time variations
        self.horizontalSlider = QtGui.QSlider(self)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.valueChanged.connect(self.plot)
        self.horizontalSlider.setMinimum(0)
        self.horizontalSlider.setMaximum(t_list.__len__()-1)

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.horizontalSlider)
        self.setLayout(layout)

        # Generate the chart for t=0 when the window is openned
        self.plot()

    def plot(self):
        # Read the slider value -> t = t_list[t_index]
        t_index = self.horizontalSlider.value()

        # Get the z-data for the given time index
        z_data = Z_xyt[t_index].flatten()

        # Discards the old chart and display the new one
        ax = self.figure.add_subplot(111,projection='3d')
        ax.hold(False)
        ax.bar3d(x_grid, y_grid, np.zeros(len(z_data)), 1, 1, z_data)

        # refresh canvas
        self.canvas.draw()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

从图形上看,窗口如下所示:

PyQt window for animating 3D bar-chars