gpt4 book ai didi

python-2.7 - 如何绘制用 def 定义的函数?

转载 作者:行者123 更新时间:2023-12-02 10:21:54 26 4
gpt4 key购买 nike

我有一个函数

np.sin(x / 2.) * np.exp(x / 4.) + 6. * np.exp(-x / 4.)

我可以使用以下代码绘制它:

x = np.arange(-5, 15, 2)
y = np.sin(x / 2.) * np.exp(x / 4.) + 6. * np.exp(-x / 4.)
plt.plot(x, y)
plt.show()

但是如果我定义函数绘图不起作用:

rr = np.arange(-5, 15, 2)

def y(o):
return np.sin(o / 2.) * np.exp(o / 4.) + 6. * np.exp(-o / 4.)

def h(b):
return int(y(b))

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

为什么会发生这种情况,如何更改代码来绘制函数?

最佳答案

试试这个:

import numpy as np
import matplotlib.pyplot as plt

rr = np.arange(-5, 15, 2)

def y(o):
return np.sin(o / 2.) * np.exp(o / 4.) + 6. * np.exp(-o / 4.)

plt.plot(rr, y(rr).astype(np.int))
plt.show()

关于python-2.7 - 如何绘制用 def 定义的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com