#題目:作 y=2x and y=x^3的函數圖形
#Python 程式碼如下
import numpy as np
import matplotlib.pyplot as plt
# Create a figure of size 8x6 inches, 80 dots per inch
plt.figure(figsize=(8, 6), dpi=80)
# Create a new subplot from a grid of 1x1
plt.subplot(1, 1, 1)
X = np.linspace(-2,2, 256, endpoint=True)
y1=2*X
y2=X**3
plt.xlim(X.min() * 1.2, X.max() * 1.2)
plt.ylim(y2.min() * 1.2, y2.max() * 1.2)
ax = plt.gca() # gca stands for 'get current axis'
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
# Plot y=2x with a blue continuous line of width 2.5 (pixels)
plt.plot(X, y1, color="blue", linewidth=2.5, linestyle="-", label="y=2x")
# Plot y=x^3 with a green continuous line of width 2.5 (pixels)
plt.plot(X, y2, color="green", linewidth=2.5, linestyle="-", label="y=x^3")
plt.legend(loc='upper right')
plt.show()
# 輸出結果如下:
# 作 y=2x and y=x^3 的函數圖形
訂閱:
文章 (Atom)