python - Numpy 指数无法正常工作

标签 python numpy matplotlib plot

我在 Python 上写了这段简单的代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-400,400,80)
y = np.exp(-1/35*np.absolute(x))

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

这是我得到的图表:

enter image description here

为什么会做错计算? 我使用 Octave 做了同样的图表:

enter image description here

谁能帮帮我,好吗?

最佳答案

当您打算使用 float 时,您正在使用整数运算。

试试这个:

y = np.exp(-1.0/35*np.absolute(x))

在 Python2 中,<int>/<int>产生一个向下舍入的整数。在 Python3 中,相同的表达式产生一个浮点值。

实现相同结果的其他方法是:

  • y = np.exp(float(1)/35*np.absolute(x))
  • from __future__ import division ... y = np.exp(1/35*np.absolute(x))
  • 使用 Python3。

关于python - Numpy 指数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367034/

相关文章:

python - numpy 数组索引 : list index and np. 数组索引给出不同的结果

python - 使用 pymodbus 从 PLC 读取时出现问题

python - 将 mkl_set_num_threads 与 numpy 一起使用

python - MatplotLib 3.0.1 中 ax.set_xlabel() 和 ax.xaxis.set_label() 的区别

python - 我应该采取哪些诊断步骤来修复此授权?

Python 数据框 : Dropping duplicates base on certain conditions

python - 解码 Base64 时出现黑色图像

python - numpy all() 评估不正确

python - 使用 pandas 或 seaborn 通过条形图制作回归线

python - 改变 matplotlib 读取图像的方式