# 数値演算、描画用ライブラリ読み込み
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 日本語表示用ライブラリ(matplotlibで日本語を使用したい場合)
import japanize_matplotlib
# グラフをinline表示可能にする
%matplotlib inline
# 解像度を上げてinline表示する
%config InlineBackend.figure_format = 'retina'
url = 'https://www.ces-alpha.org/course/file_serve/5764781927563264/weight_list.txt'
url
'https://www.ces-alpha.org/course/file_serve/5764781927563264/weight_list.txt'
df_weight = pd.read_csv(url, header=None, names=['dat'], encoding='utf-8') # utf-8 or shift-jis
df_weight
dat | |
---|---|
0 | 43.6 |
1 | 45.2 |
2 | 45.4 |
3 | 45.8 |
4 | 47.2 |
... | ... |
95 | 60.7 |
96 | 61.3 |
97 | 62.7 |
98 | 64.2 |
99 | 64.6 |
100 rows × 1 columns
weight=df_weight['dat'].to_numpy()
weight
array([43.6, 45.2, 45.4, 45.8, 47.2, 47.8, 48.2, 48.7, 48.8, 48.9, 49. , 49. , 49.4, 49.5, 49.8, 50.4, 50.5, 50.9, 50.9, 51.2, 51.2, 51.2, 51.3, 51.3, 51.6, 51.7, 51.7, 51.8, 52. , 52. , 52.1, 52.1, 52.1, 52.2, 52.3, 52.7, 52.7, 52.8, 52.9, 52.9, 53.1, 53.1, 53.8, 54. , 54.5, 54.5, 54.6, 54.7, 54.7, 54.7, 54.8, 54.9, 55.1, 55.1, 55.2, 55.3, 55.4, 55.4, 55.4, 55.6, 55.7, 55.8, 55.9, 56.1, 56.3, 56.3, 56.3, 56.4, 56.5, 56.7, 56.8, 57. , 57.1, 57.1, 57.2, 57.3, 57.6, 57.7, 57.8, 58.1, 58.4, 58.6, 58.7, 58.7, 58.7, 58.7, 59.1, 59.3, 59.9, 60. , 60.1, 60.3, 60.5, 60.6, 60.6, 60.7, 61.3, 62.7, 64.2, 64.6])
hist_ini = 43; hist_fin = 65; hist_step = 2
bins = np.arange(hist_ini, hist_fin+1, hist_step) # 終点は含まないので+1としている
# データ(weight)と階級端点(bins)を渡すと、各階級の度数が返る
dosu, _ = np.histogram(weight, bins)
cum_dosu = np.cumsum(dosu) # 累積度数(cumulative)
print(bins) # 階級両端
print(dosu) # 度数
print(cum_dosu) # 累積度数
[43 45 47 49 51 53 55 57 59 61 63 65] [ 1 3 6 9 21 12 19 15 10 2 2] [ 1 4 10 19 40 52 71 86 96 98 100]
# 基本グラフ(左軸)
plt.title("〇〇クラス男子の体重分布", fontsize=18)
plt.xlabel("体重 [kg]", fontsize=14)
plt.ylabel("生徒数", fontsize=14)
my_ticks = np.arange(hist_ini, hist_fin, hist_step)
plt.bar(my_ticks, dosu, align='edge',color="y", ec="black", width=2)
plt.xticks(np.append(my_ticks,hist_fin))
plt.yticks(np.arange(0,21,5))
plt.text(42.5, 20, f"n={dosu.sum()}", fontsize=16)
# 2軸グラフ(右軸の付加)
plt.twinx()
plt.ylim(0,110)
plt.ylabel("累積度数", fontsize=14)
plt.plot(my_ticks + hist_step/2, cum_dosu, "mD-", lw=2);
# 条件による色分け
kagen =1
jogen = 15
cond2 = ["c" if kagen <= i <= jogen else "y" for i in dosu] # 内包表記
# 基本グラフ(左軸)
plt.title("〇〇クラス男子の体重分布", fontsize=18)
plt.xlabel("体重 [kg]", fontsize=14)
plt.ylabel("生徒数", fontsize=14)
my_ticks = np.arange(hist_ini, hist_fin, hist_step)
plt.bar(my_ticks, dosu, align='edge',color=cond2, ec="black", width=2)
plt.xticks(np.append(my_ticks,hist_fin))
plt.yticks(np.arange(0,21,5))
plt.text(42.5, 20, f"n={dosu.sum()}", fontsize=16)
# 水平補助線
plt.axhline(y=kagen, c='k', ls=':')
plt.axhline(y=jogen, c='k', ls=':')
# 2軸グラフ(右軸の付加)
plt.twinx()
plt.ylim(0,110)
plt.ylabel("累積度数", fontsize=14)
plt.plot(my_ticks + hist_step/2, cum_dosu, "mD-", lw=2);
cond2
['c', 'c', 'c', 'c', 'y', 'c', 'y', 'c', 'c', 'c', 'c']
with plt.style.context('dark_background'):
# 基本グラフ(左軸)
plt.title("〇〇クラス男子の体重分布", fontsize=18)
plt.xlabel("体重 [kg]", fontsize=14)
plt.ylabel("生徒数", fontsize=14)
my_ticks = np.arange(hist_ini, hist_fin, hist_step)
plt.bar(my_ticks, dosu, align='edge',color="y", ec="black", width=2)
plt.xticks(np.append(my_ticks,hist_fin))
plt.yticks(np.arange(0,21,5))
plt.text(42.5, 20, f"n={dosu.sum()}", fontsize=16)
# 2軸グラフ(右軸の付加)
plt.twinx()
plt.ylim(0,110)
plt.ylabel("累積度数", fontsize=14)
plt.plot(my_ticks + hist_step/2, cum_dosu, "mD-", lw=2);
with plt.xkcd():
# 基本グラフ(左軸)
plt.title("Weight distributions in a class", fontsize=18)
plt.xlabel("Weight [kg]", fontsize=14)
plt.ylabel("Num of students", fontsize=14)
my_ticks = np.arange(hist_ini, hist_fin, hist_step)
plt.bar(my_ticks, dosu, align='edge',color="y", ec="black", width=2)
plt.xticks(np.append(my_ticks,hist_fin))
plt.yticks(np.arange(0,21,5))
plt.text(42.5, 20, f"n={dosu.sum()}", fontsize=16)
# 2軸グラフ(右軸の付加)
plt.twinx()
plt.ylim(0,110)
plt.ylabel("cumulative", fontsize=14)
plt.plot(my_ticks + hist_step/2, cum_dosu, "mD-", lw=2);
# 積層ヒストグラム
N = 5 # bar の数
china = [20,35,39,36,27]
usa =[25,32,34,20,25]
index = np.arange(N)
index
array([0, 1, 2, 3, 4])
p1 = plt.bar(index, china, width=0.6, color='navy')
p2 = plt.bar(index, usa, width=0.6, color='pink')
p2 = plt.bar(index, usa, width=0.6, color='pink', bottom = china)
p1 = plt.bar(index, china, width=0.6, color='navy')
p2 = plt.bar(index, usa, width=0.6, color='pink', bottom = china)
plt.title('Manufactures of products',fontsize=16)
plt.xlabel('Products',fontsize=14)
plt.ylabel('Num of manufactures',fontsize=14)
p1 = plt.bar(index, china, width=0.6, color='navy', label='China')
p2 = plt.bar(index, usa, width=0.6, color='pink', bottom = china, label='USA')
# x軸のindexを置換
plt.xticks(index, ['P1', 'P2', 'P3','P4', 'P5' ]);
plt.legend();
plt.title('Manufactures of products',fontsize=16)
plt.xlabel('Products',fontsize=14)
plt.ylabel('Num of manufactures',fontsize=14)
p1 = plt.bar(index, china, width=0.6, color='navy', label='China')
p2 = plt.bar(index, usa, width=0.6, color='pink', bottom = china, label='USA')
# x軸のindexを置換
plt.xticks(index, ['P1', 'P2', 'P3','P4', 'P5' ]);
plt.legend()
# 数値表示
plt.bar_label(p1, label_type='center', color='white', fontsize=16)
plt.bar_label(p2, label_type='center')
plt.bar_label(p2);
plt.title('Manufactures of products',fontsize=16)
plt.ylabel('Products',fontsize=14)
plt.xlabel('Num of manufactures',fontsize=14)
p1 = plt.barh(index, china, height=0.6, color='navy', label='China')
p2 = plt.barh(index, usa, height=0.6, color='pink', left = china, label='USA')
# x軸のindexを置換
plt.yticks(index, ['P1', 'P2', 'P3','P4', 'P5' ])
plt.legend()
# 数値表示
plt.bar_label(p1, label_type='center', color='white', fontsize=16)
plt.bar_label(p2, label_type='center')
plt.bar_label(p2);
# excelファイルの直接読み込み(シート名指定)
# https://diet-kenkoustyle.com/271/
df4 = pd.read_excel('dsa_data1.xlsx', sheet_name='dsa_04', header=0)
df4.head()
国語 | 数学 | 理科 | 社会 | 英語 | |
---|---|---|---|---|---|
0 | 94 | 76 | 62 | 81 | 62 |
1 | 63 | 57 | 59 | 80 | 74 |
2 | 55 | 66 | 82 | 78 | 66 |
3 | 61 | 87 | 80 | 65 | 78 |
4 | 56 | 58 | 69 | 97 | 61 |
kokugo = df4['国語'].to_numpy()
math = df4['数学'].to_numpy()
rika = df4['理科'].to_numpy()
syakai = df4['社会'].to_numpy()
eigo = df4['英語'].to_numpy()
eigo
array([ 62, 74, 66, 78, 61, 74, 82, 90, 82, 26, 85, 82, 76, 73, 64, 91, 53, 67, 54, 76, 64, 49, 70, 83, 71, 86, 100, 69, 77, 56], dtype=int64)
# 点数のリスト
subjects = [kokugo, math, rika, syakai, eigo]
# 箱ひげ図
plt.boxplot(subjects)
plt.xticks([1,2,3,4,5], ['国語', '数学', '理科', '社会', '英語'], fontsize=16)
plt.title('科目別得点分布')
plt.xlabel('科目')
plt.ylabel('点数');
# 外れ値用マーカー設定
flierprops = dict(marker="o", markerfacecolor='green', markersize=8, markeredgecolor='none')
# 胴体設定
plt.gca().boxplot(subjects,
# vert=False,
patch_artist=True,
flierprops = flierprops,
boxprops=dict(facecolor='c'),
medianprops=dict(color='k', linewidth=1)
)
plt.title('科目別得点分布', fontsize=14)
plt.xlabel('科目', fontsize=12)
plt.ylabel('得点分布', fontsize=12)
plt.xticks([1,2,3,4,5], ('国語', '数学', '理科', '社会', '英語'))
plt.ylim([0,105])
plt.grid(False)
#datetime型
import datetime
dt_now = datetime.datetime.now()
print(dt_now)
print('年:', dt_now.year)
print('月:', dt_now.month)
print('日:', dt_now.day)
print('時:', dt_now.hour)
print('分:', dt_now.minute)
print('秒:', dt_now.second)
2022-05-16 16:40:53.311897 年: 2022 月: 5 日: 16 時: 16 分: 40 秒: 53
d_today = datetime.date.today()
print(d_today)
print(d_today.year)
print(d_today.month)
print(d_today.day)
2022-05-16 2022 5 16
d = datetime.datetime.now()
print(d.strftime('%Y/%m/%d %H:%M:%S'))
print(d.strftime('%Y/%m/%d'))
print(d.strftime('%Y-%m-%d'))
print(d.strftime('%a, %b %d, %Y'))
print(('-')*50)
# 日本型表記
import locale
locale.setlocale(locale.LC_CTYPE, "Japanese_Japan.932")
print(d.strftime('%Y年%m月%d日'))
print(d.strftime('%Y年%m月%d日 %H:%M:%S'))
print(d.strftime('%Y年%m月%d日 %H時%M分%S秒'))
2022/05/16 16:40:53 2022/05/16 2022-05-16 Mon, May 16, 2022 -------------------------------------------------- 2022年05月16日 2022年05月16日 16:40:53 2022年05月16日 16時40分53秒
url='https://www.ces-alpha.org/course/file_serve/5938265186107392/dji_w.xlsx'
df_dow=pd.read_excel(url)
df_dow
Date | Open | High | Low | Close | |
---|---|---|---|---|---|
0 | 1900-01-07 | 49.34 | 49.34 | 48.24 | 48.31 |
1 | 1900-01-14 | 48.10 | 48.10 | 45.82 | 47.03 |
2 | 1900-01-21 | 46.51 | 47.29 | 46.51 | 47.29 |
3 | 1900-01-28 | 47.34 | 47.34 | 46.52 | 46.52 |
4 | 1900-02-04 | 46.68 | 49.15 | 46.68 | 49.15 |
... | ... | ... | ... | ... | ... |
6359 | 2022-04-10 | 34799.98 | 35112.21 | 34190.95 | 34721.12 |
6360 | 2022-04-17 | 34630.27 | 34889.17 | 34102.81 | 34451.23 |
6361 | 2022-04-24 | 34411.49 | 35521.00 | 33773.39 | 33811.40 |
6362 | 2022-05-01 | 33731.65 | 34106.01 | 32913.15 | 32977.21 |
6363 | 2022-05-08 | 32978.49 | 34117.74 | 32449.87 | 32899.37 |
6364 rows × 5 columns
df_dow['Close'].to_numpy()
array([ 48.31, 47.03, 47.29, ..., 33811.4 , 32977.21, 32899.37])
plt.plot(df_dow['Date'], df_dow['Close']);