使用Python制作动态3D柱状图

3
我需要展示一些数据,z = f(x,y,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个回答

1

我终于找到了一种方法,通过在PyQt窗口中嵌入图表来实现它(按照此帖子中的说明)。下面提供的代码生成一个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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接