Python:如何绘制y=cosh(x)*cos(5x)
我想用Python在我的Jupyter笔记本中为函数y=cosh(x)*cos(5x)绘制一条曲线 换言之: (x的余弦双曲线)倍(5x的余弦) 我该怎么做? 我需要进口什么? 事先非常感谢Python:如何绘制y=cosh(x)*cos(5x),python,plot,trigonometry,curve,hyperbolic-function,Python,Plot,Trigonometry,Curve,Hyperbolic Function,我想用Python在我的Jupyter笔记本中为函数y=cosh(x)*cos(5x)绘制一条曲线 换言之: (x的余弦双曲线)倍(5x的余弦) 我该怎么做? 我需要进口什么? 事先非常感谢 问候语指定所需的x值范围。 可以在Matplotlib上使用Seaborn使其更美观,但这是可选的: import seaborn as sns import matplotlib.pyplot as plt import numpy as np x = np.arange(-5,5,0.1) #
问候语指定所需的x值范围。 可以在Matplotlib上使用Seaborn使其更美观,但这是可选的:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5,5,0.1) # start,stop,step
y= (np.cosh(x))*(np.cos(5*x) )
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above)
sns.set(style="darkgrid")
plt.plot(x,y)
plt.show()
您需要导入绘图库和数学库。最常用的绘图库是
matplotlib
,对于数学来说是numpy
。对于绘图,bokeh
是matplotlib
的替代品,我认为这很好,因为默认情况下图形是交互式的。缺点是,由于它的使用不如matplotlib
广泛,因此您不太可能在StackOverflow答案和教程方面找到帮助
不管怎样,对代码:
# Import the necessary packages and modules
import matplotlib.pyplot as plt
import numpy as np
# Set your x-range and calculate y
xmin = -2.5
xmax = 2.5
numPoints = 100
x = np.linspace(xmin, xmax, numPoints)
y = np.cosh(x)*np.cos(5*x)
# Plot -- it really can be this simple [1]
plt.plot(x,y)
上面的两个图形库都为您提供了灵活的选项,用于放置轴、图例、标题等。我建议搜索关于它们的初学者教程来深入学习这些东西
[1] 在matplotlib
中打印有两种方法。这里显示的是类似MATLAB的界面。另一种方法是使用基于对象的接口,这需要更多的时间来适应,并且需要更多的样板代码,但一旦您要求对绘图的外观进行更多的控制,您将最终使用这种接口
我建议首先使用类似MATLAB的命令。文档中有一个很好的初学者教程:您尝试过什么吗?这是一个问答网站,不是一个“请求算法”网站。而且你的问题真的很简单,你甚至没有自己努力去看。你是对的,下次我应该更努力。谢谢你的反馈。非常感谢你的回答。非常感谢你的回答。如果我能给出不止一个的话,我也想给你一个绿色的钩形符号。哦,我觉得另一个的答案有点小错误。你的是唯一正确的。你得到了绿钩。