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


#make the figure
# - set up figure, set up axis(xlim,ylim), set up line as tuple for 
animation
fig = plt.figure()
ax = plt.axes(xlim=(-10,50), ylim=(1,50))
line, = ax.plot([],[], lw=2)

#initialization function - display bkgd for each frame
#line has function set data, return it as a tuple
def init():
    line.set_data([],[])
    return line,

speed = 0.01

#animation function - this is where the physics goes
def animate(i): #i is animation frames
    x = np.linspace(0,2,100) #creates set of num evenly spaces from 0,2
    y = (x - speed * i)+(((x - speed * i)^2)/2)+(((x - speed * i)^3)/6)
    line.set_data(x,y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=100, 
interval=20, blit=True)

plt.show()

I'm trying to approximate "e^x" using the infinite series definition and plot it on a graph.

For whatever reason this code produces a dialog box with a plot for a fraction of a second then ends with an exit code of 1.

I am confused as to why this does not work.

1 Answer 1

2

You have a typo when you calculate y. The exponentiation operator in Python is **, not ^. The code in the original question runs without error if you use y = (x - speed * i)+(((x - speed * i) ** 2)/2)+(((x - speed * i) ** 3)/6).


The weird thing is that you don't get the expected traceback. If you try, for example,

x = np.ones(5)
x ^ 1

you'll get a TypeError:

Traceback (most recent call last):
  File "C:/Users/joshk/GitHubProjects/stackoverflow/b.py", line 5, in <module>
    x ^ 1
TypeError: ufunc 'bitwise_xor' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

It seems like this traceback gets suppressed inside the FuncAnimator. I skimmed the source code but nothing jumped out at me as causing the suppression.

Further investigation indicates that the suppression of the traceback might be caused by PyCharm, not matplotlib.

Sign up to request clarification or add additional context in comments.

2 Comments

When running the original code, I do get exactly the TypeError: ufunc 'bitwise_xor' not supported for the input types error repeatedly for each step. So I don't think the traceback is suppressed anywhere in the matplotlib code. Might be that it is suppressed by the program used to run the code though. Nice answer anyways.
Interesting! I see the traceback when running from the command line, but not from PyCharm - must be a PyCharm problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.