■ 参考サイト
■ 参考サイト(old versionと表示されるが、こちらの方が見やすい)
# 数値演算、描画用ライブラリ読み込み
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# グラフをinline表示可能にする
%matplotlib inline
# 解像度を上げてinline表示する
%config InlineBackend.figure_format = 'retina'
# 日本語表示用ライブラリ(matplotlibで日本語を使用したい場合)
try:
import japanize_matplotlib
except ModuleNotFoundError:
# 初回だけインストールするので時間がかかる
!pip install japanize-matplotlib
import japanize_matplotlib
# データを直接打ち込む場合
a = np.array([4,7,20,9])
b = np.array([2,7,1,3])
# 横軸:a、縦軸: bとして散布図を作成
plt.scatter(a,b)
<matplotlib.collections.PathCollection at 0x23f048ef670>
# データを直接打ち込む場合
a = np.array([4,7,20,9, 11])
b = np.array([2,7,1,3,6])
if len(a) == len(b):
plt.scatter(a,b)
else:
print("データ点数が違います")
# 横軸:a、縦軸: bとして散布図を作成
# Excelファイルを読み込む
cd .
D:\Users\jupyter_notebook\DS_A
df = pd.read_excel('dsa_data1.xlsx', sheet_name ='dsa_01')
df
id | category | data1 | data2 | data3 | data_a | data_b | |
---|---|---|---|---|---|---|---|
0 | 1 | 0 | A | 45 | 23 | 23 | 58 |
1 | 2 | 2 | C | 94 | 30 | 18 | 70 |
2 | 3 | 2 | B | 53 | 19 | 10 | 70 |
3 | 4 | 2 | A | 88 | 27 | 27 | 46 |
4 | 5 | 0 | B | 57 | 22 | 14 | 40 |
5 | 6 | 0 | A | 79 | 15 | 8 | 58 |
6 | 7 | 1 | A | 53 | 17 | 20 | 40 |
7 | 8 | 1 | C | 69 | 17 | 30 | 50 |
8 | 9 | 2 | A | 80 | 20 | 22 | 42 |
9 | 10 | 0 | B | 66 | 25 | 4 | 38 |
# Pandasのデータをnumpy形式に変換
x1 = df['data2'].to_numpy()
x1
array([45, 94, 53, 88, 57, 79, 53, 69, 80, 66], dtype=int64)
y1 = df['data3'].to_numpy()
y1
array([23, 30, 19, 27, 22, 15, 17, 17, 20, 25], dtype=int64)
plt.scatter(x1, y1);
plt.scatter(x1, y1, s=500, c='#bcbd22', alpha=1, linewidth=0, edgecolor="red", marker='*');
# グラフタイトル
plt.title('散布図の作成例', fontsize=22)
plt.xlabel('x軸 (単位)', fontsize=18)
plt.ylabel('y軸 (単位)', fontsize=18)
plt.scatter(x1, y1, s=500, c='#bcbd22', alpha=1, linewidth=0, edgecolor="red", marker='*');
# グラフタイトル
plt.title('散布図の作成例', fontsize=22)
plt.xlabel('x軸 (単位)', fontsize=18)
plt.ylabel('y軸 (単位)', fontsize=18)
plt.xlim(0,100)
plt.ylim(0,40)
# 任意目盛
plt.xticks([0, 25, 50, 75, 100])
plt.yticks([0, 25, 40])
plt.grid(color='gray', linestyle='-.', linewidth=0.5)
plt.scatter(x1, y1, s=500, c='#bcbd22', alpha=1, linewidth=0, edgecolor="red", marker='*');