gpt4 book ai didi

python - 使用 Python 的动画 3D 条形图

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:32 24 4
gpt4 key购买 nike

我必须在 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()

提前致谢!

最佳答案

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

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

关于python - 使用 Python 的动画 3D 条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35415669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com