python - 如何使用 matplotlib 在 Python 中绘制 f(x) = e^x - 4x^3 + 1?
问题描述
这是我用来用 matplotlib 绘制这个函数的代码:
import math
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2,2,1000)
fx = []
for i in range (len(x)):
fx.append(math.exp(x[i]) - 4*x[i]**3 + 1)
# print(math.exp(x[i]) - 4*x[i]**3 + 1)
plt.plot(x, fx)
plt.grid()
plt.axvline()
plt.axhline()
plt.show()
以下显示了我的代码在预期图表旁边的结果:
解决方案
似乎它的绘制是正确的,只是你得到的图和图像中的图形在不同的比例上。您可以尝试将比例因子乘以 5 以获得与图像相似的结果。
multiplier = 5
xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
plt.xlim(xmin * multiplier, xmax * multiplier)
plt.ylim(ymin * multiplier, ymax * multiplier)
推荐阅读
- r - 具有不同模型规格的多组 CFA
- vue.js - webpack 4 splitChunks.maxSize - 这个值是什么意思?是KB吗?字节?只是一些价值?
- ios - 使用带有静态库的插件构建 Flutter iOS 项目时出错
- ios - 如何快速使用苹果的 Accelerate 框架来计算真实信号的 FFT?
- docusignapi - 在 Go Live 上发送文档时出现未指定的错误
- azure-devops - 每晚在调度程序上执行 DevOps Release 管道中的一个阶段
- python - 输出对带有拥抱脸/变形金刚(火炬)的 bert-base-uncased 的关注
- vue.js - vuejs中的rootState是什么,vuex上下文
- javascript - 优化我的 JavaScript lodash 代码以更快地执行
- ios - 从命令行运行时,xctest activate 不起作用