Piyush Gupta
Piyush Gupta

Reputation: 3

matplotlib reproducible plot

I am trying to plot a complex function with variable arguments in python and am finding a discrepancy I am unable to explain. My code is shown below:

import matplotlib.pyplot as plt
from numpy import pi, exp, real, imag, linspace


a = 1
b = 6
c = -14
coeff1 = 1
coeff2 = -1 / 2
coeff3 = 1j / 3


def f(t):
    return (
        coeff1 * exp(a * 1j * t) + coeff2 * exp(b * 1j * t) + coeff3 * exp(c * 1j * t)
    )


t = linspace(0, 2 * pi, 1000)

ft = f(t)
plt.plot(real(ft), imag(ft))
plt.plot(
    real(exp(1j * t) - exp(6j * t) / 2 + 1j * exp(-14j * t) / 3),
    imag(exp(1j * t) - exp(6j * t) / 2 + 1j * exp(-14j * t) / 3),
)

# These two lines make the aspect ratio square
fig = plt.gcf()
fig.gca().set_aspect("equal")

plt.show()

enter image description here

I would expect that the two functions, shown in red and green in the plot, would be identical. But this is clearly not the case. Can someone please tell me what I am missing? Thanks.

Upvotes: 0

Views: 6203

Answers (1)

Innuendo
Innuendo

Reputation: 671

Try to replace all integers with floats. Maybe it's an issue of python2 types.

On python3 I'm reproducing your code and they are equal. My dependencies versions are:

numpy==1.14.0
matplotlib==2.2.2

UPDATE. The problem is -1/2 which is equal -1 on python2

Upvotes: 1

Related Questions