import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv
from scipy.interpolate import interp1d
import locale
import japanize_matplotlib
encoding = locale.getpreferredencoding()
plt.rcParams['figure.subplot.bottom'] = 0.25
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax2 = ax1.twinx()
covid = pd.read_csv('nhk_news_covid19_domestic_daily_data.csv')
df = DataFrame(covid)
start_date = "2021/9/22"
df_s=df.set_index("日付")[start_date:]
df_r = df_s.reset_index()
df0 = pd.DataFrame(df_r)
ax1.bar(df0["日付"],df0["国内の感染者数_1日ごとの発表数"], label="国内の感染者数_1日ごとの発表数")
ax1.xaxis.set_major_locator(mdates.DayLocator(interval=5))
ax1.tick_params(axis='x', colors ="black", rotation = 90)
ax1.tick_params(axis='y', colors ="navy")
#---------------------------------------
encoding = locale.getpreferredencoding()
df_2 = pd.read_csv('effective_reproduction_number.csv', encoding="ANSI")
print(df_2)
print('\n')
#-----------------------------------
df_3 = DataFrame(df_2)
print(df_3)
df_s=df_3.set_index("日付")[start_date:]
df_r = df_s.reset_index()
print('df_r')
print(df_r)
print('\n')
print(len(df_r))
# num = df_rの行数(rows)
num = len(df_r)
df_4 = pd.DataFrame(df_r)
ml = pd.DataFrame({'no': range(num)})
print('ml')
print(ml)
df_new = pd.concat([df_4, ml], axis=1)
print('df_new')
print(df_new)
df_new.to_csv('df_new.csv', encoding = encoding)
#特定の列の抽出
df_a=df_new.loc[:,["no", "実効再生産数"]]
df_a.to_csv('df_a.csv', encoding = encoding)
interp = interp1d(df_a['no'], df_a['実効再生産数'],kind="cubic")
new_t = np.arange(0, num-1, 0.2) #range(num) num-1
#-------------------------
file = pd.DataFrame({'no': new_t, '実効再生産数':interp(new_t)})
print(file)
ax2.plot(new_t, interp(new_t), label="実効再生産数", color='red')
ax2.tick_params(colors ="red")
ax2.set_ylim(0.5, 1.2)
plt.xlim(0, num+1)
handler1, label1 = ax1.get_legend_handles_labels()
handler2, label2 = ax2.get_legend_handles_labels()
ax1.legend(handler1+handler2,label1+label2,borderaxespad=0)
ax1.grid(axis='x')
plt.plot(df_new['no'], df_new['実効再生産数'], 'o', color='orangered')
plt.grid()
plt.show()