Python 無料で独習 【超特訓】Numpy20本ノック 09

Python 無料で独習 【超特訓】Numpy20本ノック 09

Numpy20本ノック

問題その17 グラフ化


この問題からNumpyで作成したデータをMatplotlibを使ってグラフ化していきます。下記2つを実装してください。

・0~2πまでを500サンプルで区切ってsin波、cos波をプロット
・0~30までの整数からランダムに100サンプルを作成してヒストグラムにする

またMatplotlibをJupyter Notebook上で使う場合は以下の様に予め読み込みましょう
import matplotlib pyplot as plt
%matplotlib inline

とりあえず、問題の意味はよくわからないのですが、
import します。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

で、回答を見てみるとこんな記述をします。
問題でいうと、この部分です。
0~2πまでを500サンプルで区切って

x =  np.linspace(0,2*np.pi,500)
x
array([0.        , 0.01259155, 0.02518311, 0.03777466, 0.05036621,
       0.06295777, 0.07554932, 0.08814088, 0.10073243, 0.11332398,
       0.12591554, 0.13850709, 0.15109864, 0.1636902 , 0.17628175,
       0.18887331, 0.20146486, 0.21405641, 0.22664797, 0.23923952,
       0.25183107, 0.26442263, 0.27701418, 0.28960574, 0.30219729,
       0.31478884, 0.3273804 , 0.33997195, 0.3525635 , 0.36515506,
       0.37774661, 0.39033817, 0.40292972, 0.41552127, 0.42811283,
       0.44070438, 0.45329593, 0.46588749, 0.47847904, 0.4910706 ,
       0.50366215, 0.5162537 , 0.52884526, 0.54143681, 0.55402836,
       0.56661992, 0.57921147, 0.59180302, 0.60439458, 0.61698613,
       0.62957769, 0.64216924, 0.65476079, 0.66735235, 0.6799439 ,
       0.69253545, 0.70512701, 0.71771856, 0.73031012, 0.74290167,
       0.75549322, 0.76808478, 0.78067633, 0.79326788, 0.80585944,
       0.81845099, 0.83104255, 0.8436341 , 0.85622565, 0.86881721,
       0.88140876, 0.89400031, 0.90659187, 0.91918342, 0.93177498,
       0.94436653, 0.95695808, 0.96954964, 0.98214119, 0.99473274,
       1.0073243 , 1.01991585, 1.03250741, 1.04509896, 1.05769051,
       1.07028207, 1.08287362, 1.09546517, 1.10805673, 1.12064828,
       1.13323983, 1.14583139, 1.15842294, 1.1710145 , 1.18360605,
       1.1961976 , 1.20878916, 1.22138071, 1.23397226, 1.24656382,
       1.25915537, 1.27174693, 1.28433848, 1.29693003, 1.30952159,
       1.32211314, 1.33470469, 1.34729625, 1.3598878 , 1.37247936,
       1.38507091, 1.39766246, 1.41025402, 1.42284557, 1.43543712,
       1.44802868, 1.46062023, 1.47321179, 1.48580334, 1.49839489,
       1.51098645, 1.523578  , 1.53616955, 1.54876111, 1.56135266,
       1.57394422, 1.58653577, 1.59912732, 1.61171888, 1.62431043,
       1.63690198, 1.64949354, 1.66208509, 1.67467664, 1.6872682 ,
       1.69985975, 1.71245131, 1.72504286, 1.73763441, 1.75022597,
       1.76281752, 1.77540907, 1.78800063, 1.80059218, 1.81318374,
       1.82577529, 1.83836684, 1.8509584 , 1.86354995, 1.8761415 ,
       1.88873306, 1.90132461, 1.91391617, 1.92650772, 1.93909927,
       1.95169083, 1.96428238, 1.97687393, 1.98946549, 2.00205704,
       2.0146486 , 2.02724015, 2.0398317 , 2.05242326, 2.06501481,
       2.07760636, 2.09019792, 2.10278947, 2.11538103, 2.12797258,
       2.14056413, 2.15315569, 2.16574724, 2.17833879, 2.19093035,
       2.2035219 , 2.21611346, 2.22870501, 2.24129656, 2.25388812,
       2.26647967, 2.27907122, 2.29166278, 2.30425433, 2.31684588,
       2.32943744, 2.34202899, 2.35462055, 2.3672121 , 2.37980365,
       2.39239521, 2.40498676, 2.41757831, 2.43016987, 2.44276142,
       2.45535298, 2.46794453, 2.48053608, 2.49312764, 2.50571919,
       2.51831074, 2.5309023 , 2.54349385, 2.55608541, 2.56867696,
       2.58126851, 2.59386007, 2.60645162, 2.61904317, 2.63163473,
       2.64422628, 2.65681784, 2.66940939, 2.68200094, 2.6945925 ,
       2.70718405, 2.7197756 , 2.73236716, 2.74495871, 2.75755027,
       2.77014182, 2.78273337, 2.79532493, 2.80791648, 2.82050803,
       2.83309959, 2.84569114, 2.85828269, 2.87087425, 2.8834658 ,
       2.89605736, 2.90864891, 2.92124046, 2.93383202, 2.94642357,
       2.95901512, 2.97160668, 2.98419823, 2.99678979, 3.00938134,
       3.02197289, 3.03456445, 3.047156  , 3.05974755, 3.07233911,
       3.08493066, 3.09752222, 3.11011377, 3.12270532, 3.13529688,
       3.14788843, 3.16047998, 3.17307154, 3.18566309, 3.19825465,
       3.2108462 , 3.22343775, 3.23602931, 3.24862086, 3.26121241,
       3.27380397, 3.28639552, 3.29898708, 3.31157863, 3.32417018,
       3.33676174, 3.34935329, 3.36194484, 3.3745364 , 3.38712795,
       3.3997195 , 3.41231106, 3.42490261, 3.43749417, 3.45008572,
       3.46267727, 3.47526883, 3.48786038, 3.50045193, 3.51304349,
       3.52563504, 3.5382266 , 3.55081815, 3.5634097 , 3.57600126,
       3.58859281, 3.60118436, 3.61377592, 3.62636747, 3.63895903,
       3.65155058, 3.66414213, 3.67673369, 3.68932524, 3.70191679,
       3.71450835, 3.7270999 , 3.73969146, 3.75228301, 3.76487456,
       3.77746612, 3.79005767, 3.80264922, 3.81524078, 3.82783233,
       3.84042389, 3.85301544, 3.86560699, 3.87819855, 3.8907901 ,
       3.90338165, 3.91597321, 3.92856476, 3.94115631, 3.95374787,
       3.96633942, 3.97893098, 3.99152253, 4.00411408, 4.01670564,
       4.02929719, 4.04188874, 4.0544803 , 4.06707185, 4.07966341,
       4.09225496, 4.10484651, 4.11743807, 4.13002962, 4.14262117,
       4.15521273, 4.16780428, 4.18039584, 4.19298739, 4.20557894,
       4.2181705 , 4.23076205, 4.2433536 , 4.25594516, 4.26853671,
       4.28112827, 4.29371982, 4.30631137, 4.31890293, 4.33149448,
       4.34408603, 4.35667759, 4.36926914, 4.3818607 , 4.39445225,
       4.4070438 , 4.41963536, 4.43222691, 4.44481846, 4.45741002,
       4.47000157, 4.48259312, 4.49518468, 4.50777623, 4.52036779,
       4.53295934, 4.54555089, 4.55814245, 4.570734  , 4.58332555,
       4.59591711, 4.60850866, 4.62110022, 4.63369177, 4.64628332,
       4.65887488, 4.67146643, 4.68405798, 4.69664954, 4.70924109,
       4.72183265, 4.7344242 , 4.74701575, 4.75960731, 4.77219886,
       4.78479041, 4.79738197, 4.80997352, 4.82256508, 4.83515663,
       4.84774818, 4.86033974, 4.87293129, 4.88552284, 4.8981144 ,
       4.91070595, 4.92329751, 4.93588906, 4.94848061, 4.96107217,
       4.97366372, 4.98625527, 4.99884683, 5.01143838, 5.02402993,
       5.03662149, 5.04921304, 5.0618046 , 5.07439615, 5.0869877 ,
       5.09957926, 5.11217081, 5.12476236, 5.13735392, 5.14994547,
       5.16253703, 5.17512858, 5.18772013, 5.20031169, 5.21290324,
       5.22549479, 5.23808635, 5.2506779 , 5.26326946, 5.27586101,
       5.28845256, 5.30104412, 5.31363567, 5.32622722, 5.33881878,
       5.35141033, 5.36400189, 5.37659344, 5.38918499, 5.40177655,
       5.4143681 , 5.42695965, 5.43955121, 5.45214276, 5.46473432,
       5.47732587, 5.48991742, 5.50250898, 5.51510053, 5.52769208,
       5.54028364, 5.55287519, 5.56546675, 5.5780583 , 5.59064985,
       5.60324141, 5.61583296, 5.62842451, 5.64101607, 5.65360762,
       5.66619917, 5.67879073, 5.69138228, 5.70397384, 5.71656539,
       5.72915694, 5.7417485 , 5.75434005, 5.7669316 , 5.77952316,
       5.79211471, 5.80470627, 5.81729782, 5.82988937, 5.84248093,
       5.85507248, 5.86766403, 5.88025559, 5.89284714, 5.9054387 ,
       5.91803025, 5.9306218 , 5.94321336, 5.95580491, 5.96839646,
       5.98098802, 5.99357957, 6.00617113, 6.01876268, 6.03135423,
       6.04394579, 6.05653734, 6.06912889, 6.08172045, 6.094312  ,
       6.10690356, 6.11949511, 6.13208666, 6.14467822, 6.15726977,
       6.16986132, 6.18245288, 6.19504443, 6.20763598, 6.22022754,
       6.23281909, 6.24541065, 6.2580022 , 6.27059375, 6.28318531])

なんだかわかりにくいですが、
0 から 2×π←2×3.14 までを500で区切をしています。

これをsin   cos でプロットします。

plt.plot(x , np.sin(x))
plt.plot(x , np.cos(x))
plt.show()


次は
・0~30までの整数からランダムに100サンプルを作成してヒストグラムにする
の問題です。
ランダムなデータを作成します。

#0~30までの数字でランダムに100個つくる
data = np.random.randint(0,30 ,100)
data
array([27, 25,  3, 24, 11,  5,  6,  7, 23, 13,  5,  6, 24, 20,  6,  4, 28,
       19,  7, 26,  7, 14,  3, 21, 12,  4, 18,  9, 17, 23,  1, 15, 28,  9,
       13, 15, 12, 18, 15, 29, 28,  2, 17, 20, 17,  9,  3,  6, 16, 12, 12,
       14,  3, 10,  8,  2, 11, 23, 12, 24,  9, 26, 19, 28,  4,  9, 23,  8,
       23, 27, 17,  7,  9, 29, 17,  0, 22, 11, 16, 13, 12, 17, 25, 29, 19,
       10, 21, 23, 27, 11,  3, 23, 22,  2, 10,  8, 19,  9,  1, 13])

そしてヒストグラムを作成します。

plt.hist(data)
(array([ 6., 10., 11., 14., 12., 11.,  8., 11.,  7., 10.]),
 array([ 0. ,  2.9,  5.8,  8.7, 11.6, 14.5, 17.4, 20.3, 23.2, 26.1, 29. ]),
 <BarContainer object of 10 artists>)

プログラミン独習カテゴリの最新記事