python - matplotlib 和阶乘函数

标签 python python-3.x matplotlib factorial

有没有直接的方法使用 matplotlib 绘制阶乘函数?我想绘制 x^5 和 45n!一起。

我尝试使用

import matplotlib.pyplot as plt
import numpy as np
import math
x = np.linspace(0, 10, 1000)
plt.plot(x, x**5)
plt.plot(x, 45*math.factorial(x))

但是阶乘部分没有绘制出来。有什么想法吗?

最佳答案

关于您的代码有两件事:

  1. 我认为 math 函数只接受标量(intfloat 等),而不接受列表或 numpy 数组。

  2. 阶乘函数仅针对(正)整数定义,不适用于 float ,例如0.5

我认为您正在寻找 gamma function ,它将阶乘函数扩展到实数:

from scipy.special import gamma

x = np.linspace(0, 10, 1000)
plt.plot(x,gamma(x), label='Factorial')
plt.plot(x, x**5, label='$x^5$')

xx = np.arange(11)
plt.scatter(xx, gamma(xx))
plt.legend()

输出: enter image description here

关于python - matplotlib 和阶乘函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58633026/

相关文章:

python - 应用数据帧方法后 numpy 数组末尾的额外数据类型

python-3.x - 基于 Spacy 规则匹配器选择 Pandas DataFrame 的行

python - Matplotlib事件处理: when do they send matplotlib events and when Qt events

python - matplotlib - 使用多轴时定位 xlabel/ylabel(hist + scatter)

python - 为什么这个 python 脚本不会输入到我设置的 MySQL 表中?

python - 当 URL 模式吸收它时忽略 `/static`

python - 更新应用引擎实体

python脚本高级调度

python - 从字典中调用函数

python - matplotlib - 通过 **kwargs 传递行参数时出错