在matplotlib / python中绘制多条曲线

时间:2022-09-10 22:02:35

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?

我试图绘制下面定义的三条不同的曲线(修改的NACA曲线),但是我实现的代码为f(y)生成与f(w)和f(z)相同的结果。绘图导致相同的曲线3次。谁能指出我哪里出错了?

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()

1 个解决方案

#1


1  

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.

函数仅由其名称引用。就Python而言,使用不同的输入参数集并不会使它成为一个独特的函数。因此,每次声明函数f时,都会覆盖以前的版本。因此,最后你只剩下f(w),并且你的所有情节都会重复调用。

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).

现在,这意味着也可以在不同的函数中重复使用x作为输入,但是符号表示可能不是你想要的(假设这是一个缩短的例子,因为这里没有实际使用符号) 。

#1


1  

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.

函数仅由其名称引用。就Python而言,使用不同的输入参数集并不会使它成为一个独特的函数。因此,每次声明函数f时,都会覆盖以前的版本。因此,最后你只剩下f(w),并且你的所有情节都会重复调用。

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).

现在,这意味着也可以在不同的函数中重复使用x作为输入,但是符号表示可能不是你想要的(假设这是一个缩短的例子,因为这里没有实际使用符号) 。