# 数値演算、描画用ライブラリ読み込み
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# グラフをinline表示可能にする
%matplotlib inline
# 解像度を上げてinline表示する
%config InlineBackend.figure_format = 'retina'
# 日本語表示用ライブラリ(matplotlibで日本語を使用したい場合)
import japanize_matplotlib
# Excelファイルを読み込む
df = pd.read_excel('dsa_data1.xlsx', sheet_name ='dsa_02', skiprows = 0)
df
temp_2020 | sales_2020 | temp_2021 | sales_2021 | |
---|---|---|---|---|
0 | 28 | 520 | 29 | 540 |
1 | 32 | 570 | 35 | 590 |
2 | 35 | 600 | 33 | 630 |
3 | 33 | 630 | 31 | 640 |
4 | 27 | 490 | 26 | 490 |
5 | 24 | 520 | 22 | 550 |
6 | 30 | 500 | 32 | 540 |
7 | 38 | 730 | 39 | 740 |
8 | 33 | 610 | 33 | 650 |
9 | 21 | 440 | 23 | 460 |
10 | 24 | 420 | 20 | 450 |
11 | 22 | 450 | 20 | 480 |
12 | 29 | 560 | 27 | 580 |
13 | 35 | 620 | 32 | 670 |
14 | 33 | 500 | 34 | 550 |
df.head()
temp_2020 | sales_2020 | temp_2021 | sales_2021 | |
---|---|---|---|---|
0 | 28 | 520 | 29 | 540 |
1 | 32 | 570 | 35 | 590 |
2 | 35 | 600 | 33 | 630 |
3 | 33 | 630 | 31 | 640 |
4 | 27 | 490 | 26 | 490 |
df.tail()
temp_2020 | sales_2020 | temp_2021 | sales_2021 | |
---|---|---|---|---|
10 | 24 | 420 | 20 | 450 |
11 | 22 | 450 | 20 | 480 |
12 | 29 | 560 | 27 | 580 |
13 | 35 | 620 | 32 | 670 |
14 | 33 | 500 | 34 | 550 |
df.head()
temp_2020 | sales_2020 | temp_2021 | sales_2021 | |
---|---|---|---|---|
0 | 28 | 520 | 29 | 540 |
1 | 32 | 570 | 35 | 590 |
2 | 35 | 600 | 33 | 630 |
3 | 33 | 630 | 31 | 640 |
4 | 27 | 490 | 26 | 490 |
temp_2020 = df['temp_2020'].to_numpy()
sales_2020 = df['sales_2020'].to_numpy()
temp_2021 = df['temp_2021'].to_numpy()
sales_2021 = df['sales_2021'].to_numpy()
temp_2020, sales_2020, temp_2021, sales_2021
(array([28, 32, 35, 33, 27, 24, 30, 38, 33, 21, 24, 22, 29, 35, 33], dtype=int64), array([520, 570, 600, 630, 490, 520, 500, 730, 610, 440, 420, 450, 560, 620, 500], dtype=int64), array([29, 35, 33, 31, 26, 22, 32, 39, 33, 23, 20, 20, 27, 32, 34], dtype=int64), array([540, 590, 630, 640, 490, 550, 540, 740, 650, 460, 450, 480, 580, 670, 550], dtype=int64))
# 複数系列データの散布図描画(様々な修飾付加)
plt.xlim(15.0, 40.0)
plt.ylim(300, 800)
plt.title('売上高と気温の関係 (2020-2021)',fontsize=18)
plt.xlabel("気温 (℃)", fontsize=20)
plt.ylabel("売上高 ($)", fontsize=20)
plt.grid(True, zorder=1)
plt.tick_params(labelsize = 14)
# グラフの描画
plt.scatter(temp_2020, sales_2020, s=80, c="b", marker="D", alpha=0.5, label="2020", zorder=2)
plt.scatter(temp_2021, sales_2021, s=80, c="r", marker="D", alpha=0.5, label="2021", zorder=3)
plt.legend(loc="lower right", fontsize=14);
# 複数系列データの散布図描画(様々な修飾付加)
plt.xlim(15.0, 40.0)
plt.ylim(300, 800)
plt.title('売上高と気温の関係 (2020-2021)',fontsize=18)
plt.xlabel("気温 (℃)", fontsize=20)
plt.ylabel("売上高 ($)", fontsize=20)
plt.grid(True, zorder=1)
plt.tick_params(labelsize = 14)
# グラフの描画
plt.scatter(temp_2020, sales_2020, s=400, c="g", marker="$\clubsuit$", alpha=0.5, label="2020", zorder=2)
plt.scatter(temp_2021, sales_2021, s=400, c="r", marker="$\clubsuit$", alpha=0.5, label="2021", zorder=3)
plt.legend(loc="lower right", fontsize=14)
# グラフの保存
plt.savefig('test2.png', bbox_inches='tight', dpi=360, transparent=True);
# 複数系列データの散布図描画(様々な修飾付加)
plt.xlim(15.0, 40.0)
plt.ylim(300, 800)
plt.title('売上高と気温の関係 (2020-2021)',fontsize=18)
plt.xlabel("気温 (℃)", fontsize=20)
plt.ylabel("売上高 ($)", fontsize=20)
plt.grid(True, zorder=1)
plt.tick_params(labelsize = 14)
#plt.text(16, 750, "n=15", fontsize=16)
plt.text(16, 750, f"n={len(temp_2020)}", fontsize=16) # f文字列
# グラフの描画
plt.scatter(temp_2020, sales_2020, s=400, c="g", marker="$\clubsuit$", alpha=0.5, label="2020", zorder=2)
plt.scatter(temp_2021, sales_2021, s=400, c="r", marker="$\clubsuit$", alpha=0.5, label="2021", zorder=3)
plt.legend(loc="lower right", fontsize=14)
# グラフの保存
plt.savefig('test2.png', bbox_inches='tight', dpi=360, transparent=True);
len(temp_2020)
15
sales_2021
array([540, 590, 630, 640, 490, 550, 540, 740, 650, 460, 450, 480, 580, 670, 550], dtype=int64)
plt.plot(sales_2021)
[<matplotlib.lines.Line2D at 0x218c16bfd30>]
plt.plot(temp_2021, sales_2021)
[<matplotlib.lines.Line2D at 0x218c135b040>]
plt.plot(temp_2021, sales_2021, marker='o')
[<matplotlib.lines.Line2D at 0x218c1228e80>]
plt.plot(temp_2021, sales_2021, marker='o', linestyle='--')
[<matplotlib.lines.Line2D at 0x218bf128fd0>]
plt.plot(temp_2021, sales_2021, marker='o', linestyle='', c='m')
[<matplotlib.lines.Line2D at 0x218c1325f60>]
# color, marker, linestyle はこの順でまとめてsetできる
plt.title('売上高と気温の関係 (2020-2021)',fontsize=18)
plt.xlabel("気温 (℃)", fontsize=20)
plt.ylabel("売上高 ($)", fontsize=20)
plt.plot(temp_2021, sales_2021,'g^--', markersize=12);
# Excelファイルを読み込む
df = pd.read_excel('dsa_data1.xlsx', sheet_name ='dsa_03', skiprows = 0)
df
age | boy_height | boy_weight | girl_height | girl_weight | |
---|---|---|---|---|---|
0 | 1 | 77.8 | 10.1 | 78.4 | 10.0 |
1 | 2 | 88.7 | 12.5 | 87.2 | 12.1 |
2 | 3 | 96.7 | 14.3 | 96.2 | 14.1 |
3 | 4 | 103.2 | 16.6 | 101.2 | 15.6 |
4 | 5 | 110.9 | 18.7 | 108.3 | 17.2 |
5 | 6 | 115.3 | 20.5 | 113.1 | 20.2 |
6 | 7 | 121.6 | 23.4 | 121.1 | 22.0 |
7 | 8 | 127.4 | 25.9 | 127.1 | 25.4 |
8 | 9 | 133.6 | 29.6 | 133.1 | 29.9 |
9 | 10 | 137.3 | 32.7 | 138.5 | 32.6 |
10 | 11 | 145.7 | 38.0 | 146.8 | 38.0 |
11 | 12 | 152.3 | 42.9 | 150.8 | 39.7 |
12 | 13 | 159.3 | 47.6 | 155.5 | 46.1 |
13 | 14 | 164.3 | 51.2 | 157.2 | 47.2 |
14 | 15 | 167.5 | 55.6 | 155.9 | 48.1 |
15 | 16 | 168.9 | 58.2 | 157.8 | 50.5 |
16 | 17 | 172.4 | 64.6 | 155.0 | 49.6 |
17 | 18 | 170.5 | 60.7 | 158.8 | 53.4 |
18 | 19 | 174.9 | 68.0 | 157.0 | 50.5 |
19 | 20 | 173.2 | 64.5 | 156.4 | 50.0 |
20 | 21 | 170.7 | 68.5 | 156.6 | 50.2 |
21 | 22 | 170.7 | 66.1 | 158.1 | 51.0 |
22 | 23 | 172.9 | 71.3 | 156.1 | 52.5 |
23 | 24 | 171.3 | 64.1 | 159.5 | 51.5 |
24 | 25 | 170.9 | 61.1 | 160.4 | 53.1 |
df.head()
age | boy_height | boy_weight | girl_height | girl_weight | |
---|---|---|---|---|---|
0 | 1 | 77.8 | 10.1 | 78.4 | 10.0 |
1 | 2 | 88.7 | 12.5 | 87.2 | 12.1 |
2 | 3 | 96.7 | 14.3 | 96.2 | 14.1 |
3 | 4 | 103.2 | 16.6 | 101.2 | 15.6 |
4 | 5 | 110.9 | 18.7 | 108.3 | 17.2 |
age = df['age'].to_numpy() # 下の4データ(y)に対して共通
boy_height = df['boy_height'].to_numpy()
boy_weight = df['boy_weight'].to_numpy()
girl_height = df['girl_height'].to_numpy()
girl_weight = df['girl_weight'].to_numpy()
age, boy_height, boy_weight, girl_height, girl_weight
(array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25], dtype=int64), array([ 77.8, 88.7, 96.7, 103.2, 110.9, 115.3, 121.6, 127.4, 133.6, 137.3, 145.7, 152.3, 159.3, 164.3, 167.5, 168.9, 172.4, 170.5, 174.9, 173.2, 170.7, 170.7, 172.9, 171.3, 170.9]), array([10.1, 12.5, 14.3, 16.6, 18.7, 20.5, 23.4, 25.9, 29.6, 32.7, 38. , 42.9, 47.6, 51.2, 55.6, 58.2, 64.6, 60.7, 68. , 64.5, 68.5, 66.1, 71.3, 64.1, 61.1]), array([ 78.4, 87.2, 96.2, 101.2, 108.3, 113.1, 121.1, 127.1, 133.1, 138.5, 146.8, 150.8, 155.5, 157.2, 155.9, 157.8, 155. , 158.8, 157. , 156.4, 156.6, 158.1, 156.1, 159.5, 160.4]), array([10. , 12.1, 14.1, 15.6, 17.2, 20.2, 22. , 25.4, 29.9, 32.6, 38. , 39.7, 46.1, 47.2, 48.1, 50.5, 49.6, 53.4, 50.5, 50. , 50.2, 51. , 52.5, 51.5, 53.1]))
# 身長
plt.title('成長曲線',fontsize=18)
plt.xlabel("年齢", fontsize=20)
plt.ylabel("身長 (cm)", fontsize=20)
plt.plot(age, boy_height, 'co-', label='boy')
plt.plot(age, girl_height, 'mo-', label='girl')
plt.legend(fontsize=14);
# 体重
plt.title('成長曲線',fontsize=18)
plt.xlabel("年齢", fontsize=20)
plt.ylabel("体重 (kg)", fontsize=20)
plt.plot(age, boy_weight, 'co-', label='boy')
plt.plot(age, girl_weight, 'mo-', label='girl')
plt.legend(fontsize=14);
# 身長
plt.title('成長曲線',fontsize=18)
plt.xlabel("年齢", fontsize=20)
plt.ylabel("身長 (cm)", fontsize=20)
plt.plot(age, boy_height, 'co-', label='boy')
plt.plot(age, girl_height, 'mo-', label='girl')
plt.legend(fontsize=14);
# 2軸目の設定 (twinx)
# 体重
plt.twinx()
plt.title('成長曲線',fontsize=18)
plt.xlabel("年齢", fontsize=20)
plt.ylabel("体重 (kg)", fontsize=20)
plt.plot(age, boy_weight, 'cD-', label='boy')
plt.plot(age, girl_weight, 'mD-', label='girl')
plt.legend(fontsize=14);
# 複数グラフ一括描画例
plt.figure(figsize=(10, 6))
plt.subplot(2, 2, 1)
plt.plot(age, boy_height, c='c')
plt.grid()
plt.ylim(60, 180)
plt.xlabel('Age',fontsize=14)
plt.ylabel('男子身長 [cm]', fontsize=14)
plt.subplot(2, 2, 2)
plt.plot(age, boy_weight, c='b')
plt.grid()
plt.ylim(0, 80)
plt.xlabel('Age',fontsize=14)
plt.ylabel('男子体重 [kg]', fontsize=14)
plt.subplot(2, 2, 3)
plt.plot(age, girl_height, c='m')
plt.grid()
plt.ylim(60, 180)
plt.xlabel('Age',fontsize=14)
plt.ylabel('女子身長 [cm]', fontsize=14)
plt.tight_layout()
plt.subplot(2, 2, 4)
plt.plot(age, girl_weight, c='r')
plt.grid()
plt.ylim(0, 80)
plt.xlabel('Age',fontsize=14)
plt.ylabel('女子体重 [kg]', fontsize=14)
plt.tight_layout();
# グラフタイトル
plt.title('Effects of excitation power', fontsize=18)
# 軸名(y軸:TeX表記を使用)
plt.xlabel('Power (mW)', fontsize=16)
plt.ylabel('Generation rate ($s^{-1}$)', fontsize=16)
# 理論値の描画
power = np.linspace(0,10,51) # x軸データの準備:0-10の範囲を、51点分割
# print(power) # just for check
rate = power**2
# zorder は、重ねる際の順序(数字の大きい方が上になる)
plt.plot(power, rate,"c-", markersize=10, lw=1.5,label='theory', zorder=1)
# 実験値(平均値)の描画
x_marker = np.arange(0, 11, 2)
y_marker = x_marker**2
plt.scatter(x_marker, y_marker, s=60, marker='D',color='r', lw=2, label='experimental', zorder=3)
# 実験値(標準偏差)の描画
#err = np.array([1/2,1,2,3,4,5])*3
err = np.array([4,4,4,4,4,4])*2
plt.errorbar(x_marker, y_marker, yerr=err, c='g', ls='', capsize=5, zorder=2)
# 目盛線のラベルサイズ
plt.tick_params(labelsize = 12)
# 軸範囲の指定を追加
plt.xlim(-.5,10.5)
plt.ylim(-10,120)
# 凡例表示
plt.legend(fontsize=12, loc='upper left');
# グリッドの表示
plt.grid(False)
from scipy.stats import norm
center = 0; sigma = 1
z_pos = 1
#Both sides
x1 = np.linspace(center - 4*sigma, center + 4*sigma, 2**8)
y1 = norm.pdf(x1, loc=center, scale=sigma)
x2 = np.linspace(center - z_pos*sigma, center + z_pos*sigma, 2**8)
y2 = norm.pdf(x2, loc=center, scale=sigma)
plt.plot(x1, y1, color="c")
plt.plot(x2, y2, color="c")
plt.fill_between(x2, 0, y2, facecolor='c', alpha=0.3)
plt.vlines(x=-z_pos, ymin=-0, ymax=norm.pdf(z_pos,loc=center,scale=sigma),
linestyles='dashed', linewidths=1)
plt.vlines(x=z_pos, ymin=-0, ymax=norm.pdf(z_pos,loc=center,scale=sigma),
linestyles='dashed', linewidths=1)
plt.xlabel("$\sigma$ (standard deviation)", fontsize=14)
plt.ylabel("Probability", fontsize=14)
prob = 2*norm.cdf(x=z_pos, loc=center, scale=sigma)-1
print(f"区間 [{-z_pos: .1f}*sigma,{z_pos: .1f}*sigma]に入る確率:{prob: .3f}")
区間 [-1.0*sigma, 1.0*sigma]に入る確率: 0.683
# グラフタイトル
plt.title('Effects of excitation power', fontsize=18)
# 軸名(y軸:TeX表記を使用)
plt.xlabel('Power (mW)', fontsize=16)
plt.ylabel('Generation rate ($s^{-1}$)', fontsize=16)
# データ(平均値)の描画
x_marker = np.arange(0, 11, 2)
y_marker = x_marker**2
plt.plot(x_marker, y_marker, ms=5, marker=None, color='r', lw="2")
# データ(標準偏差)の描画
y_err = np.array([1/2,1,2,3,4,5])*3
x_err = np.array([1/2,1,2,3,4,5])/10
plt.errorbar(x_marker, y_marker, yerr = y_err, xerr = x_err, capsize=5, fmt='o',
markersize=10, ecolor='black', markeredgecolor = "black", color='w')
# 目盛線のラベルサイズ
plt.tick_params(labelsize = 12)
# 軸範囲の指定を追加
plt.xlim(-.5,11)
plt.ylim(-10,120)
# グリッドの表示
plt.grid(False)
理論値のような数学関数を描画する場合、横軸には等間隔の配列(ndarray)を用いる。 NumPyで等間隔の配列を
生成するには、 np.arange()
または np.linspace()
を用いる。
arange()は間隔(公差)を指定、linspace()は要素数を指定という違いがあり、目的によって使い分ける。
# x軸の作成 (linspace):範囲とデータ点数の指定
x_data1 = np.linspace(-10,10,21)
print(x_data1)
# x軸の作成 (arange):範囲と間隔(公差)の指定
x_data2 = np.arange(-10,10.5,0.5)
print(x_data2)
[-10. -9. -8. -7. -6. -5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] [-10. -9.5 -9. -8.5 -8. -7.5 -7. -6.5 -6. -5.5 -5. -4.5 -4. -3.5 -3. -2.5 -2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5 10. ]
# 数学関数の上にマーカーを散布図として追加してみる
# 数学関数の定義(横軸データはx_data1, x_data2 いずれも可)
y_math = x_data1**2 - 10
# グラフ描画
plt.plot(x_data1, y_math, 'c', zorder=1)
plt.xlim(-12,12) # x軸範囲
plt.ylim(-20,100) # y軸範囲
plt.grid()
# マーカ描画用の散布図を作成して重ねる
x_marker = np.arange(-10, 11, 2)
y_marker = x_marker**2 - 10
plt.scatter(x_marker, y_marker, s=600, alpha=0.5, zorder=2, marker='$\heartsuit$',lw=2, edgecolors="red")
# グラフタイトルと軸名
plt.title('Drawing a graph (plot)', fontsize=20)
plt.xlabel('x-axis', fontsize=20)
plt.ylabel('y-axis', fontsize=20);
数学関数の描画
from sympy import plot, Symbol
x = Symbol('x')
plot(x**2*(x**2-4), (x, -2.4, 2.4));