set.seed(2)

u <- rnorm(n = 10, mean =  0, sd = 1) 
v <- rnorm(n = 10, mean =  2, sd = 2) 
w <- rnorm(n = 10, mean = -2, sd = 3) 
d <- round(data.frame(u, v, w), 2)


fr <- as.POSIXct("2022-08-11 12:00:00") 
to <- as.POSIXct("2022-08-20 12:00:00") 


px <- seq(fr, to, by = "days")

rownames(d) <- px

nr <- nrow(d) 
nc <- ncol(d) 

write.csv(d, file = "timeseries_py.csv", quote = F)

library(DT)
datatable(d, caption = "正規乱数の時系列データ")
COL <- c(rgb(255,   0,   0,  255, max = 255),
         rgb(  0,   0, 255,  255, max = 255),
         rgb(  0, 155,   0,  255, max = 255))
matplot(x = px, y = d, type = "l")

matplot(x = px, y = d, type = "o", pch = colnames(d))

matplot(x = px, y = d, type = "o", pch = 1:nc, xlab = "2021", ylab = "値")

grid()

legend("bottomright", legend = colnames(d), pch = 1:nc, lty = 1:nc, col =1:nc)

matplot(x = px,            
        y = d,            
        type = "n",        
        xaxt = "n",        
        ylim = c(-12, 12), 
        main = "時系列グラフ(出版品質)",
        xlab = "x軸 ラベル [単位]",
        ylab = "y軸 ラベル [単位]")


px.g <- seq(px[1], px[nr], by = 60*60*24*2)


axis(side = 1, at = px.g, labels = format(px.g, "%b/%d(%a)"))


abline(lty = 2,                
       col = gray(0.5, 0.25), 
       h   = seq(-50, 50, 5), 
       v   = px.g)            


matlines(x = px, y = d$u, type = "o", pch = 1, lty = 1, col = COL[1])
matlines(x = px, y = d$v, type = "o", pch = 2, lty = 2, col = COL[2])
matlines(x = px, y = d$w, type = "o", pch = 3, lty = 3, col = COL[3])


legend("topright", col = COL, pch = 1:nc, lty = 1:nc, legend = colnames(d))

library(plotly)
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout
kyokasho <- list(size = 11, color = "blue", family = "UD Digi Kyokasho NK-R")

plot_ly(mode = "lines+markers") |>
  add_trace(x = px, y = d$u, name = "u") |>
  add_trace(x = px, y = d$v, name = "v") |>
  add_trace(x = px, y = d$w, name = "w") |>
  config(locale = "ja") |> 
  layout(font = kyokasho,
         title = "インタラクティブグラフ",
         xaxis = list(title = "x軸ラベル[単位]"),
         yaxis = list(title = "y軸ラベル[単位]"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
import pandas as pd

d = pd.read_csv('timeseries_py.csv')
d = d.rename(columns = {'Unnamed: 0': 'px'}) 

d['px'] = pd.to_datetime(d['px'])
d = d.set_index('px')
print(d)
##                         u     v     w
## px                                   
## 2022-08-11 12:00:00 -0.90  2.84  4.27
## 2022-08-12 12:00:00  0.18  3.96 -5.60
## 2022-08-13 12:00:00  1.59  1.21  2.77
## 2022-08-14 12:00:00 -1.13 -0.08  3.86
## 2022-08-15 12:00:00 -0.08  5.56 -1.99
## 2022-08-16 12:00:00  0.13 -2.62 -9.36
## 2022-08-17 12:00:00  0.71  3.76 -0.57
## 2022-08-18 12:00:00 -0.24  2.07 -3.79
## 2022-08-19 12:00:00  1.98  4.03  0.38
## 2022-08-20 12:00:00 -0.14  2.86 -1.13
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

plt.gca().xaxis.set_major_formatter(
  mdates.DateFormatter("%b/%d (%a)"))

plt.gca().xaxis.set_major_locator(
  mdates.DayLocator(interval = 2))

plt.xticks(fontsize = 10)
## (array([1.]), [Text(1.0, 0, '1/02 (金)')])

plt.title('作図')
plt.xlabel('x軸ラベル[単位]')
plt.ylabel('y軸ラベル[単位]')

plt.grid(linestyle = '--', color = (0.9, 0.9, 0.9, 0.25))

plt.plot(d['u'], label = 'u', marker = 'o', linestyle = 'solid')
plt.plot(d['v'], label = 'v', marker = '^', linestyle = 'dotted')
plt.plot(d['w'], label = 'w', marker = '*', linestyle = 'dashed')

plt.legend(loc = 'lower right')

plt.show()
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 36600 (\N{CJK UNIFIED IDEOGRAPH-8EF8}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12521 (\N{KATAKANA LETTER RA}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12505 (\N{KATAKANA LETTER BE}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12523 (\N{KATAKANA LETTER RU}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65339 (\N{FULLWIDTH LEFT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 21336 (\N{CJK UNIFIED IDEOGRAPH-5358}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65341 (\N{FULLWIDTH RIGHT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20316 (\N{CJK UNIFIED IDEOGRAPH-4F5C}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\naruk\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 22259 (\N{CJK UNIFIED IDEOGRAPH-56F3}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)

d0 <- read.csv(file = 'C:/Users/naruk/Downloads/electric_power.csv')

d <- data.frame(y1 = d0$y1, y2 = d0$y2)
rownames(d) <- d0$X

px <- as.POSIXct(d0$x)