Matplotlib set_data 不显示一条线

在我以前的代码版本中,我清除了绘图并重新绘制了数据直到最近计算的数据点。为了加快绘图速度,我试图切换到使用set_data,所以我没有清除每一步。不幸的是,我无法根据此更改生成一行。


下面你可以看到我尝试使用set_data左上角的图形,但剩下的我使用我原来的方法。我希望将此用于我的图形的所有部分,但我不确定此方法是否适用于补丁。


请让我知道如何解决此问题。


import numpy as np

import matplotlib.pyplot as plt

import matplotlib.patches as patches

import matplotlib.gridspec as gridspec

 

# =============================================================================

# Parameters

# =============================================================================

SIM_TIME = 10

STEP_SIZE = 0.05

steps = int(SIM_TIME/STEP_SIZE)

STEPS_PER_FRAME = 4

order = 4  # Two second order equations

 

ICs = [0., 0., 0., 1., 0.]  # Intial conditions; t0, x1, x1dot, x2, x2dot


parameters = {'a':[1.0,  'unit'],

              'b':[2.0,  'unit'],

              'c':[3.0,  'unit']}


# =============================================================================

# Intializing Arrays

# =============================================================================

x_and_v = np.empty(shape=(order, steps))  # Each row is a var, i.e. x1,x2,...

 

# Set initial conditions for each var

for i in range(order):

    x_and_v[i][0] = ICs[i+1]

    

K = np.empty(shape=(4, order)) # Each row is k1, k2, k3, k4 for each var

t = np.empty(steps)

t[0] = ICs[0]


# =============================================================================

# ODE function

# =============================================================================

def ODE(t, curr):

    dx1dt = parameters['a'][0]*t

    dv1dt = parameters['b'][0]*t

    dx2dt = parameters['a'][0]*t

    dv2dt = parameters['c'][0]*t    

    return np.array([dx1dt, dv1dt, dx2dt, dv2dt])



蝴蝶刀刀
浏览 100回答 1
1回答

隔江千里

我在这个问题上的错误实际上是一个非常基本的错误和对set_data功能的误解。我以为我需要传递一个新的数据点,但实际上你需要传递整个数据集,但要有更新的点。所以,代码最终看起来如下:import numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as patchesimport matplotlib.gridspec as gridspec&nbsp;# =============================================================================# Parameters# =============================================================================SIM_TIME = 10STEP_SIZE = 0.05steps = int(SIM_TIME/STEP_SIZE)STEPS_PER_FRAME = 4order = 4&nbsp; # Two second order equations&nbsp;ICs = [0., 0., 0., 1., 0.]&nbsp; # Intial conditions; t0, x1, x1dot, x2, x2dotparameters = {'a':[1.0,&nbsp; 'unit'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'b':[2.0,&nbsp; 'unit'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'c':[3.0,&nbsp; 'unit']}# =============================================================================# Intializing Arrays# =============================================================================x_and_v = np.empty(shape=(order, steps))&nbsp; # Each row is a var, i.e. x1,x2,...&nbsp;# Set initial conditions for each varfor i in range(order):&nbsp; &nbsp; x_and_v[i][0] = ICs[i+1]&nbsp; &nbsp;&nbsp;K = np.empty(shape=(4, order)) # Each row is k1, k2, k3, k4 for each vart = np.empty(steps)t[0] = ICs[0]# =============================================================================# ODE function# =============================================================================def ODE(t, curr):&nbsp; &nbsp; dx1dt = parameters['a'][0]*t&nbsp; &nbsp; dv1dt = parameters['b'][0]*t&nbsp; &nbsp; dx2dt = parameters['a'][0]*t&nbsp; &nbsp; dv2dt = parameters['c'][0]*t&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return np.array([dx1dt, dv1dt, dx2dt, dv2dt])# =============================================================================# Runge-Kutta (4th Order) Method# =============================================================================def RK4(i):&nbsp; &nbsp; # calculates each k value&nbsp; &nbsp; K[0] = STEP_SIZE * ODE(t[i], x_and_v[:, i])&nbsp; &nbsp; K[1] = STEP_SIZE * ODE(t[i] + STEP_SIZE/2, x_and_v[:, i] + K[0]/2)&nbsp; &nbsp; K[2] = STEP_SIZE * ODE(t[i] + STEP_SIZE/2, x_and_v[:, i] + K[1]/2)&nbsp; &nbsp; K[3] = STEP_SIZE * ODE(t[i] + STEP_SIZE, x_and_v[:, i] + K[2])&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return 1/6 * (K[0] + 2*K[1] + 2*K[2] + K[3])# =============================================================================# Plotting function# =============================================================================plt.close('all')plt.ion()fig = plt.figure(figsize=(10, 12))fig.suptitle(f'Title (stepsize: {STEP_SIZE})',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y=0.94)gs = gridspec.GridSpec(2, 2)graph_left = fig.add_subplot(gs[0, 0])graph_right = fig.add_subplot(gs[0, 1], sharey=graph_left)graph_left_x_line, = graph_left.plot(np.array([]), np.array([]),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label='Position',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='#1248a1')graph_left_v_line, = graph_left.plot(np.array([]), np.array([]),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label='Velocity',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='#77a7f7')graph_left.set_ylabel('position [m]/velocity [m/s]')graph_left.set_xlabel('time [s]')graph_left.legend(loc=2)graph_right_x_line, = graph_right.plot(np.array([]), np.array([]),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label='Position',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='#ba7000')graph_right_v_line, = graph_right.plot(np.array([]), np.array([]),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label='Velocity',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='#f7c477')graph_right.set_xlabel('time [s]')graph_right.legend(loc=2)block = fig.add_subplot(gs[1, :])block.set_title('Block Animation')block.set_xlabel('Position [m]')def Plotting(i):&nbsp; &nbsp; """Plotting x_and_v with time counter in the top middle"""&nbsp; &nbsp; graph_left_x_line.set_data(t[:i], x_and_v[0,:i])&nbsp; &nbsp; graph_left_v_line.set_data(t[:i], x_and_v[1,:i])&nbsp; &nbsp; graph_left.relim()&nbsp; &nbsp; graph_left.autoscale_view()&nbsp; &nbsp; graph_right_x_line.set_data(t[:i], x_and_v[2, :i])&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; graph_right_v_line.set_data(t[:i], x_and_v[3, :i])&nbsp; &nbsp; graph_right.relim()&nbsp; &nbsp; graph_right.autoscale_view()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; """Animated blocks and spring with time counter in the top middle"""&nbsp; &nbsp; block.cla()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; m1x = x_and_v[0][i]&nbsp; &nbsp; m2x = x_and_v[2][i]&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; side1 = parameters['a'][0]&nbsp; &nbsp; side2 = parameters['b'][0]&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; block.set_ylim(0, max(side1, side2))&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; mass1 = patches.Rectangle(&nbsp; &nbsp; &nbsp; &nbsp; (m1x - side1, 0),&nbsp; &nbsp; &nbsp; &nbsp; side1, side1,&nbsp; &nbsp; &nbsp; &nbsp; facecolor='#1f77b4')&nbsp; &nbsp; mass2 = patches.Rectangle(&nbsp; &nbsp; &nbsp; &nbsp; (m2x, 0),&nbsp; &nbsp; &nbsp; &nbsp; side2, side2,&nbsp; &nbsp; &nbsp; &nbsp; facecolor='#ff7f0e')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; spring = patches.ConnectionPatch(xyA=(m1x, min(side1, side2)/2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coordsA='data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xyB=(m2x, min(side1, side2)/2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coordsB='data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;linewidth=2, color='k')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; block.add_patch(spring)&nbsp; &nbsp; block.add_patch(mass1)&nbsp; &nbsp; block.add_patch(mass2)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; block.annotate(f'time = {round(t[i],1)}s', xy=(0.5, 0.98),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xycoords='axes fraction', ha='center', va='top')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; block.set_title('Block Animation')&nbsp; &nbsp; block.set_xlabel('Position [m]')&nbsp; &nbsp; block.axis('equal')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; fig.canvas.draw()&nbsp; &nbsp; plt.pause(0.01)# =============================================================================# Main loop that calculates each x and v value using RK 4th order method# =============================================================================i = 0while i < (steps-1):&nbsp; &nbsp; x_and_v[:, i+1] = x_and_v[:, i] + RK4(i)&nbsp; &nbsp; t[i+1] = t[i] + STEP_SIZE&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if i % STEPS_PER_FRAME == 0:&nbsp; &nbsp; &nbsp; &nbsp; Plotting(i)&nbsp; &nbsp; i += 1print('Done')&nbsp;plt.show()# plt.close()&nbsp; # closes the plot at then end
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python