python 繪制三角函數
Trigonometry is one of the most important parts in engineering and many times, therefore matplotlib.pyplot in combination with NumPy can help us to plot our desired trigonometric functions. In this article, we are going to introduce a few examples of trig-functions.
三角學是工程學中最重要的部分之一,因此, matplotlib.pyplot與NumPy結合可以幫助我們繪制所需的三角函數。 在本文中,我們將介紹一些三角函數的例子。
1)正弦函數 (1) Sine Function)
s = np.sin(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='Sin(x)',
title='Sine Plot')
ax.grid()
plt.show()
2)余弦函數 (2) Cosine Function)
s = np.cos(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='(cosx)',
title='Cosine Plot')
ax.grid()
plt.show()

3)切線函數 (3) Tangent Function)
t = np.arange(0.0, 1, 0.01)
s = np.tan(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='tan(x)',
title='Tangent Plot')
ax.grid()
plt.show()
用于繪制三角函數的Python代碼 (Python code for plotting trigonometric functions)
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0, 24, 0.01)
# Sine Plot
s = np.sin(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='Sin(x)',
title='Sine Plot')
ax.grid()
plt.show()
# Cosine Plot
s = np.cos(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='(cosx)',
title='Cosine Plot')
ax.grid()
plt.show()
# Tangent Plot
t = np.arange(0.0, 1, 0.01)
s = np.tan(t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='radians', ylabel='tan(x)',
title='Tangent Plot')
ax.grid()
plt.show()
Output:
輸出:
Output is as figure
翻譯自: https://www.includehelp.com/python/plotting-trigonometric-functions.aspx
python 繪制三角函數