简体   繁体   English

Python:如何 plot y=cosh(x) *cos(5x)

[英]Python: How to plot y=cosh(x) *cos(5x)

using Python I would like to plot a curve for the function y=cosh(x)*cos(5x) in my Jupyter Notebook.使用 Python 我想 plot 在我的 Jupyter 笔记本中为 function y=cosh(x)*cos(5x) 绘制曲线

In other words: (cosine hyperbolicus of x) times (cosine of 5x)换句话说:(x 的余弦双曲线)乘以(5x 的余弦)

How do I do this?我该怎么做呢? What do I need to import?我需要导入什么? Thank you very much in advance.非常感谢您提前。

Greetings问候

Specify the range of values for x that you need.指定所需的 x 值范围。 You can use Seaborn on top of Matplotlib to make it prettier, but this is optional:您可以在 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()

在此处输入图像描述

You will need to import a plotting library and a maths library.您将需要导入绘图库和数学库。 The most commonly used plotting library is matplotlib , and for maths it's numpy .最常用的绘图库是matplotlib ,数学是numpy For plotting, bokeh is a an alternative to matplotlib , which I think is great because graphs are interactive by default.对于绘图, bokehmatplotlib的替代品,我认为这很好,因为默认情况下图表是交互式的。 The disadvantage is that because it's not as widely used as matplotlib , you're less likely to find help on it in terms of StackOverflow answers and tutorials.缺点是因为它没有像matplotlib那样广泛使用,所以您不太可能在 StackOverflow 答案和教程方面找到帮助。

Anyway, to the code:无论如何,代码:

# 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)

Both of the graphing libraries above give you flexible options on where to place the axes, legends, titles, and so on.上面的两个图形库都为您提供了关于放置轴、图例、标题等的灵活选项。 I recommend searching for beginner's tutorials on them to learn this stuff in depth.我建议搜索有关它们的初学者教程以深入学习这些内容。

[1] There are two ways to plot in matplotlib . [1] matplotlib 中的matplotlib有两种方式。 What is shown here is the MATLAB-like interface.这里显示的是类似 MATLAB 的界面。 The other method is to use the object-based interface, which takes a bit more of getting used to, and requires a bit more boilerplate code, but that's what you will end up using once you demand more control over the appearance of your plots.另一种方法是使用基于对象的接口,这需要更多时间来适应,并且需要更多样板代码,但是一旦您需要更多地控制绘图的外观,您最终将使用这种方法。

I recommend starting with the MATLAB-like commands first.我建议先从类似 MATLAB 的命令开始。 The documentation has a good beginner's tutorial: https://matplotlib.org/stable/tutorials/introductory/pyplot.html文档有很好的初学者教程: https://matplotlib.org/stable/tutorials/introductory/pyplot.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM