Skip to content

Instantly share code, notes, and snippets.

@801YutaKa108
Created April 26, 2020 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 801YutaKa108/a91db96d556aaff270cf1257a7ad8b06 to your computer and use it in GitHub Desktop.
Save 801YutaKa108/a91db96d556aaff270cf1257a7ad8b06 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
# linspace(start, stop)
x = np.linspace(0, 10)
# x → array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,
# 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,
# 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,
# 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,
# 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,
# 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,
# 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,
# 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,
# 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,
# 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
# x.size → 50
# 簡単なグラフ作成例
x = np.linspace(-2, 2)
y = x**2
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, y)
plt.show()
# linspace(start, stop, num)
x = np.linspace(0, 3.14, 5)
# x → array([0. , 0.785, 1.57 , 2.355, 3.14 ])
# endpointの指定
x1 = np.linspace(0, 10, 5, endpoint=True)
# x → array([ 0. , 2.5, 5. , 7.5, 10. ])
x2 = np.linspace(0, 10, 5, endpoint=False)
# y → array([0., 2., 4., 6., 8.])
# pyplotで出力
plt.plot(x1, np.zeros_like(x), "o", label="endpoint=True")
plt.plot(x2, np.zeros_like(x)+0.5, "o", label="endpoint=False")
plt.ylim([-0.5, 1])
plt.show()
# retstepでstep取得
x, step = np.linspace(0, 3.14, 5, retstep=True)
# x → array([0. , 0.785, 1.57 , 2.355, 3.14 ])
# step → 0.785
# startとstopを配列で指定
x = np.linspace([0,10], [5,20], 3, axis=0)
# x → array([[ 0. , 10. ],
# [ 2.5, 15. ],
# [ 5. , 20. ]])
x = np.linspace([0,10], [5,20], 3, axis=1)
# x → array([[ 0. , 2.5, 5. ],
# [10. , 15. , 20. ]])
# arangeとlinspaceの違い
x = np.arange(0, 100, 25)
# x → array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
y = np.linspace(0, 100, 10)
# y → array([ 0. , 11.11111111, 22.22222222, 33.33333333,
# 44.44444444, 55.55555556, 66.66666667, 77.77777778,
# 88.88888889, 100. ])
# arangeとlinspaceの違い(最小限の引数)
x = np.arange(0, 50)
# x → array([ 0, 1, 2, 3,..., 49]) #1ずつ増える
y = np.linspace(0, 10)
# y → array([ 0. , 0.20408163, 0.40816327,..., 10.]) #50要素
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment