Matplotlib глюк [python3] - PullRequest
0 голосов
/ 11 апреля 2020

H каждый, я пытаюсь сделать простую функцию matplotlib graphi c для представления функций зарядки и разрядки конденсатора. Вот программа:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

import math

x_data = [0] # storing the variable values
y_data = [0] # storing the dependent variable value

fig, ax = plt.subplots() # creating a subplot and its axes
ax.set_xlim(0, 20) # setting the x axe limits
ax.set_ylim(0, 12) # setting the y axe limits
line, = ax.plot(0, 0) # create a line object

# creating an function to animate
def animation_frame(t):

    x_data.append(t) # append the variable to x_data arrya

    # print a sort of differential of the function
    print(y_data[len(y_data) - 1] - y_data[len(y_data) - 2])

    # check if the differential is so small that can be considered as 0 (local maximum point)
    if y_data[len(y_data) - 1] - y_data[len(y_data) - 2] >= 0.0012 or y_data[len(y_data) - 1] - y_data[len(y_data) - 2] == 0.0:

        # if it isn't continue plotting the charging function
        y_data.append(9 * ( 1 - (np.e)**(-(t/(180000*0.00001)))))

    else:
        # plot the discharging function
        y_data.append(9 * (np.e)**(-(t/(180000*0.00001))))

    # set the line points coordinates
    line.set_xdata(x_data)
    line.set_ydata(y_data)

run the animation function 
animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 10, 0.05), interval=10)

# show the plot
plt.show()

Не имеет значения тип функции, потому что даже если в else из условия if-else (в функции animate) я вставляю простой тождество (f (t ) = t), это не будет работать, и это глючит, вот некоторые изображения:

Final glitch using the f(t) = t function

Final glitch using the f(t) = V0 * e^(-t/RC) discharge function

Я подозреваю, что проблема находится внутри части дифференциальной проверки, в функции анимации ... Но я не знаю, как ее решить. У вас есть идеи?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...