Numpy と Matplotlib
モジュールのインポート
import numpy as np
import matplotlib.pyplot as plt
ヒストグラム
正規分布を求めると,
a5 = np.random.randn(100) # 正規分布 (平均=0,rms=1)
a5
array([-1.12043857, 1.09802794, -0.1685271 , 0.98953488, -2.25716891,
0.61066463, 0.64150313, -0.6288412 , -1.13584117, -0.58864403,
0.1486943 , -0.14231748, 0.32401251, 0.45296463, -1.60306967,
0.73369299, -0.60021034, 0.9378989 , 1.74294973, -0.11231528,
-1.58556023, 1.34674471, 2.51275461, 0.55214047, 1.74925534,
0.1059542 , 0.59144226, 0.35080744, 1.31094271, 1.51834575,
0.01604619, -0.10526502, -1.10306706, 0.99475848, 0.40230207,
-0.61901879, 0.97373688, -0.37541703, -0.99382981, 1.01716118,
-2.23456286, 0.14788868, 0.99531633, -0.97070253, -0.24885368,
0.87012452, -0.85726725, 0.88145497, 0.06155496, 0.23439447,
-0.42681647, -0.73384209, 0.14339033, 0.32060922, 2.44967485,
-0.15768328, -1.00372234, -0.75520285, 0.11067972, -0.5582602 ,
0.66276622, -1.02047694, -0.66513015, 0.70457846, -0.7269394 ,
-0.50467771, 1.00826353, -1.13600297, -0.13293978, 1.09467057,
-1.15480398, 0.69554842, 1.86269524, 0.63230495, -0.86172479,
0.24582258, 1.14461494, 1.2052686 , 0.6837642 , -0.72078725,
0.60427866, 0.29872362, -0.79730124, -0.48720224, 0.66220885,
-0.48147771, -1.3523074 , 0.80362587, 1.69392196, 1.66506637,
-0.33294617, -1.07237561, -1.13451646, 0.08126804, 0.75022476,
-0.09087122, 1.55252189, -0.53577613, 0.39723474, 0.37985521])
これをヒストグラム
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
として描くと,
fig = plt.figure()
plt.hist(a5) # histogram
(array([ 2., 3., 13., 17., 14., 16., 20., 8., 5., 2.]),
array([-2.25716891, -1.78017656, -1.30318421, -0.82619186, -0.34919951,
0.12779285, 0.6047852 , 1.08177755, 1.5587699 , 2.03576226,
2.51275461]),
<BarContainer object of 10 artists>)
fig.savefig("hist_ex1.pdf")
fig = plt.figure()
plt.hist(a5, rwidth=0.8) # histogram
(array([ 2., 3., 13., 17., 14., 16., 20., 8., 5., 2.]),
array([-2.25716891, -1.78017656, -1.30318421, -0.82619186, -0.34919951,
0.12779285, 0.6047852 , 1.08177755, 1.5587699 , 2.03576226,
2.51275461]),
<BarContainer object of 10 artists>)
fig.savefig("hist_ex2.pdf")
a50 = np.random.randn(10000) # 正規分布 (平均=0, rms=1)
fig = plt.figure()
plt.hist(a50, rwidth=0.8, bins=30) # histogram
(array([2.00e+00, 4.00e+00, 1.00e+01, 1.90e+01, 3.10e+01, 7.40e+01,
1.32e+02, 1.85e+02, 2.46e+02, 4.64e+02, 5.47e+02, 7.00e+02,
8.68e+02, 1.01e+03, 9.75e+02, 9.74e+02, 8.82e+02, 8.21e+02,
6.48e+02, 4.84e+02, 3.58e+02, 2.19e+02, 1.66e+02, 9.10e+01,
4.60e+01, 2.40e+01, 1.40e+01, 4.00e+00, 1.00e+00, 1.00e+00]),
array([-3.74329663, -3.48859127, -3.2338859 , -2.97918053, -2.72447516,
-2.4697698 , -2.21506443, -1.96035906, -1.7056537 , -1.45094833,
-1.19624296, -0.94153759, -0.68683223, -0.43212686, -0.17742149,
0.07728388, 0.33198924, 0.58669461, 0.84139998, 1.09610535,
1.35081071, 1.60551608, 1.86022145, 2.11492681, 2.36963218,
2.62433755, 2.87904292, 3.13374828, 3.38845365, 3.64315902,
3.89786439]),
<BarContainer object of 30 artists>)
fig.savefig("hist_ex3.pdf")
プロット
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
a7 = np.arange(0,1,0.02) # 等間隔の1次元配列(0から1-0.02)
a7
array([0. , 0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 ,
0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38, 0.4 , 0.42,
0.44, 0.46, 0.48, 0.5 , 0.52, 0.54, 0.56, 0.58, 0.6 , 0.62, 0.64,
0.66, 0.68, 0.7 , 0.72, 0.74, 0.76, 0.78, 0.8 , 0.82, 0.84, 0.86,
0.88, 0.9 , 0.92, 0.94, 0.96, 0.98])
delta = 0.01
a77 = np.arange(0,1+delta,delta) # 等間隔の1次元配列(0から1-0.02)
a77
array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,
0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,
0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,
0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,
0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
0.99, 1. ])
fig = plt.figure()
plt.plot(a7, a7**2)# x-y plot
fig.savefig("plot_ex1.pdf")
plt.show()
fig = plt.figure()
plt.plot(a7, a7**2)# x-y plot
plt.plot(a77, a77**3)# x-y plot
fig.savefig("plot_ex2.pdf")
plt.show()
関数
def func(x):
return 2*(x-0.5)*(x-2)*(x-3.5)
を用いて計算し,グリッド付けると
x = np.linspace(0,4,101) # 等間隔の1次元配列(0から4)
fig = plt.figure()
plt.plot(x, func(x))# x-y plot
plt.grid()
fig.savefig("plot_ex3.pdf")
plt.show()
等高線図
メッシュグリッドより,座標データを格子状に準備する.
x = np.linspace(-2,2,101)
y = np.linspace(-2,2,101)
xv, yv = np.meshgrid(x,y)
f = np.exp(-(xv**2+yv**2)) * np.sin(xv)
正方形の図として描き,
fig = plt.figure()
plt.contourf(xv, yv, f, levels=10)
plt.colorbar()
plt.axis('square')
fig.savefig("contourf_ex1.pdf")
plt.show()