問題:「如何使用Python來繪製三角函數圖形呢?」
- 9.2.1. Number-theoretic and representation functions
- 9.2.2. Power and logarithmic functions
- 9.2.3. Trigonometric functions
- 9.2.4. Angular conversion
- 9.2.5. Hyperbolic functions
- 9.2.6. Special functions
- 9.2.7. Constants
在Trigonometric functions一節中可查到底下三個:
math.
cos
(x)- Return the cosine of x radians.
math.
sin
(x)- Return the sine of x radians.
math.
tan
(x)- Return the tangent of x radians.
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# 產生x座標的資料,從 -PI 到 PI | |
X = np.linspace(-np.pi, np.pi, 260, endpoint=True) | |
# Y座標 | |
Cos_y, Sin_y = np.cos(X), np.sin(X) | |
Tan_y = np.tan(X) | |
plt.plot(X, Cos_y, color="red", linewidth=1.0, linestyle="-", label="Cos") | |
plt.plot(X, Sin_y, color="green", linewidth=1.0, linestyle="--", label="Sin") | |
plt.plot(X, Tan_y, color="blue", linewidth=1.0, linestyle=":", label="Tan") | |
# 設定 y 座標範圍 | |
plt.ylim(-2.0, 2.0) | |
# 設定 x 座標文字 | |
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], | |
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) | |
plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.) | |
plt.show() |
輸出圖:
沒有留言:
張貼留言