データ

# x軸データとして,1,2,...,100の数列をxに与える。
# データサイズはnに代入する。
x <- 1:100
n <- length(x)

set.seed(5) # 乱数固定

# 正規乱数のカラムu,v,wを持つデータフレームを作成する。
d <- data.frame(
  u = rnorm(n, mean =  0, sd = 1), # サイズn,平均 0,標準偏差1の正規乱数
  v = rnorm(n, mean =  2, sd = 2), # サイズn,平均 2,標準偏差2の正規乱数
  w = rnorm(n, mean = -2, sd = 3)) # サイズn,平均-2,標準偏差3の正規乱数
# カラーパレット
# R(Red),G(Green),B(Blue),透過率の4つの数字で色を表現する。
COL <- c(rgb(255,   0,   0,  105, max = 255), # 赤
         rgb(  0,   0, 255,  105, max = 255), # 青
         rgb(  0, 155,   0,  105, max = 255)) # 緑

[RGB_Color] https://www.rapidtables.com/web/color/RGB_Color.html


散布図(簡易)

x値が共通の3つの変数をまとめてプロット
x座標の値がデータごとに異ならない(同じ)場合

  1. マーカーのみ

pch: point character(1文字記号のマーカー)

matplot(x, d, type = 'p', pch = colnames(d))

  1. マーカー+ラベル+格子線+凡例
matplot(x, d, type = 'p', pch = 16, col = COL,
        main = '主タイトル',
        xlab = 'x軸ラベル[単位]',
        ylab = 'y軸ラベル[単位]')

# 格子線(grid lines)
grid()
 
# 凡例(はんれい)
legend('topleft', pch = 16, col = COL, legend = colnames(d))

散布図(x座標値がデータ間で異なる場合)

例)
データ1(座標):\((x_1, y_1)\)
データ2(座標):\((x_2, y_2)\)
データ3(座標):\((x_3, y_3)\)

d.x <- data.frame(x1 = 1:3, x2 = 4:6, x3 = 3:5) # x座標だけ集めた
d.y <- data.frame(y1 = 3:1, y2 = 2:0, y3 = 7:9) # y座標だけ集めた
matplot(x = d.x, y = d.y)

散布図(出版品質)

個別にプロット(x座標の値がデータごとに異なる場合)
はじめに図枠を描き,その後にデータを個別にプロットする。

# 図枠
matplot(x = x, y = d, type = 'n', # typeは'n'でプロットなし
        ylim = c(-10, 10),
        main = '主タイトル',
        xlab = 'x軸ラベル[単位]',
        ylab = 'y軸ラベル[単位]')

# 格子線(grid lines)
abline(lty = 2,               # 線種(2:点線)
       col = gray(0.5, 0.25), # 灰色(濃さ,透過率)
       h = seq(-10, 10, 5),   # 水平線; horizontal lines
       v = seq(0, 100, 20))   # 垂直線; vertical lines

# 作図
PCH <- 16:18 # マーカーを番号で指定
matpoints(x = x, y = d$u, pch = PCH[1], col = COL[1])
matpoints(x = x, y = d$v, pch = PCH[2], col = COL[2])
matpoints(x = x, y = d$w, pch = PCH[3], col = COL[3])
 
# 凡例(はんれい)
legend('topright', pch = PCH, col = COL, legend = colnames(d))

複数の変数ペアの散布図(一度に見たいときに便利)

pairs(d, pch = 16, col = COL[1])

library(plotly)

# フォント設定
kyokasho <- list(size = 11, color = 'blue', family = 'UD Digi Kyokasho NK-R')

plot_ly() |>
  add_trace(x = x, y = d$u, name = 'u', marker = list(color = COL[1])) |>
  add_trace(x = x, y = d$v, name = 'v', marker = list(color = COL[2])) |>
  add_trace(x = x, y = d$w, name = 'w', marker = list(color = COL[3])) |>
  layout(barmode = 'group',
         font  = kyokasho,
         title = '主タイトル',
         xaxis = list(title = 'x軸ラベル[単位]'),
         yaxis = list(title = 'y軸ラベル[単位]', range = c(-10, 10)))

plotly::line-and-scatter

Python

データ

import numpy as np
import pandas as pd

np.random.seed(3)
u = np.random.normal(loc =-2.0, scale = 1.0, size = 100)
v = np.random.normal(loc = 0.0, scale = 2.0, size = 100)
w = np.random.normal(loc = 2.0, scale = 3.0, size = 100)

x = np.arange(1, 101, 1) 

作図

import matplotlib.pyplot as plt

# ラベル
plt.title('主タイトル')
plt.xlabel('x軸ラベル[単位]')
plt.ylabel('y軸ラベル[単位]')

# 格子線(grid lines)
plt.grid(linestyle = '--', color = (0.9, 0.9, 0.9, 0.25))

# プロット
plt.scatter(x, u, color = (1.0, 0.0, 1.0, 0.25), label = 'u', marker = 'o')
plt.scatter(x, v, color = (0.0, 1.0, 0.0, 0.25), label = 'v', marker = '^')
plt.scatter(x, w, color = (0.0, 0.0, 1.0, 0.25), label = 'w', marker = '*')

# 凡例(はんれい)
plt.legend(loc = 'upper left')

# 作図
plt.show()