chopboy
chopboy

Reputation: 25

plotting multiple curves in matplotlib / python

I am trying to plot three different curves (Modified NACA profiles) defined below, however the code I have implemented generates the same result for f(y) as it does for f(w) and f(z). The plots result in the same curve 3 times. Could anyone please point out where I've gone wrong?

Cheers.

import sympy as sy
import numpy as np
import matplotlib.pyplot as plt

x, z, w, a0, a1, a2, a3, n, c, = symbols('x z w a0 a1 a2 a3 n c ')


def f(x):
    return 2.2268*(x)**(1/2)+2.6295*x-0.0217*(x)**2+5.7406*10**(-5)*(x)**3

def f(z):
    return 2.2268*(z)**(1/2)+1.5821*z-8.2664*10**(-3)*(z)**2+1.3718*10**(-5)*(z)**3

def f(w):
    return 2.2268*(w)**(1/2)+1.1139*w-4.2846*10**(-3)*(w)**2+5.1828*10**(-6)*(w)**3

x = np.arange(0., 300, 0.01)
y = np.arange(0., 300, 0.01)
z = np.arange(0., 300, 0.01)
w = np.arange(0., 300, 0.01)

plt.plot(x, f(x), )
plt.show()


plt.plot(z, f(z), )
plt.show()


plt.plot(w, f(w), )
plt.show()

plt.plot(x, f(x), z, f(z), w, f(w), )
plt.show()

Upvotes: 1

Views: 1761

Answers (1)

JohanL
JohanL

Reputation: 6891

A function is referenced by its name only. Using different set of input parameters does not make it a unique function as far as Python is concerned. As a consequence, each time you declare function f, you overwrite the previous version. Thus, in the end you are left with only f(w) and this is called repeatedly for all your plots.

The easiest way to fix this is to give your functions different names:

import sympy as sy
import numpy as np
import matplotlib.pyplot as plt

x, z, w, a0, a1, a2, a3, n, c, = sy.symbols('x z w a0 a1 a2 a3 n c ')


def f(x):
    return 2.2268*(x)**(1/2)+2.6295*x-0.0217*(x)**2+5.7406*10**(-5)*(x)**3

def g(z):
    return 2.2268*(z)**(1/2)+1.5821*z-8.2664*10**(-3)*(z)**2+1.3718*10**(-5)*(z)**3

def h(w):
    return 2.2268*(w)**(1/2)+1.1139*w-4.2846*10**(-3)*(w)**2+5.1828*10**(-6)*(w)**3

x = np.arange(0., 300, 0.01)
y = np.arange(0., 300, 0.01)
z = np.arange(0., 300, 0.01)
w = np.arange(0., 300, 0.01)

plt.plot(x, f(x), )
plt.show()


plt.plot(z, g(z), )
plt.show()


plt.plot(w, h(w), )
plt.show()

plt.plot(x, f(x), z, g(z), w, h(w), )
plt.show()

Now, this means it would also be possible to re-use x as input in the different functions, but then the symbolic representation might not be exactly what you want (assuming this is a shortened example, since the symbols are not actually used here).

Upvotes: 1

Related Questions