今回は…
前に作った感染者グラフ表示ツール
https://qiita.com/kuroge/items/0894dc2858fc0e2c2862
を強化してみました
今回使うライブラリ
・pandas
・matplotlib
インストールは
python3 -m pip install matplotlib
または
pip install matplotlib
コードと収集する情報
前のコード
ビフォー
import pandas
from matplotlib import pyplot
hyouji = ["ALL","Hokkaido","Aomori","Iwate","Miyagi","Akita","Yamagata","Fukushima","Ibaraki","Tochigi","Gunma","Saitama","Chiba","Tokyo","Kanagawa","Niigata","Toyama","Ishikawa","Fukui","Yamanashi","Nagano","Gifu","Shizuoka","Aichi","Mie","Shiga","Kyoto","Osaka","Hyogo","Nara","Wakayama","Tottori","Shimane","Okayama","Hiroshima","Yamaguchi","Tokushima","Kagawa","Ehime","Kochi","Fukuoka","Saga","Nagasaki","Kumamoto","Oita","Miyazaki","Kagoshima","Okinawa"]
for i in hyouji:
pyplot.figure()
pyplot.title(f"{i}")
#pyplot.rcParams["figure.figsize"] = (10, 80)
score = pandas.read_csv("https://covid19.mhlw.go.jp/public/opendata/newly_confirmed_cases_daily.csv",encoding="utf-8")
score2 = score[i]
date = [score["Date"][a][0:6] for a in range(0,len(score["Date"]))]
pyplot.xticks(rotation=30)
#pyplot.yticks(np.arange(0, 100000, step=1000))
pyplot.bar(date, score2)
pyplot.savefig(f"コロナ - {i}.png")
はい
pcが遅くなったり落ちたりする原因に満ちていますね
例えば
・毎回CSVを読みに行ってること
・一回一回完全にリセットされてないこと
今のコード
アフター
import pandas
from matplotlib import pyplot
hyouji = ["ALL",
"Hokkaido",
"Aomori",
"Iwate",
"Miyagi",
"Akita",
"Yamagata",
"Fukushima",
"Ibaraki",
"Tochigi",
"Gunma",
"Saitama",
"Chiba",
"Tokyo",
"Kanagawa",
"Niigata",
"Toyama",
"Ishikawa",
"Fukui",
"Yamanashi",
"Nagano",
"Gifu",
"Shizuoka",
"Aichi",
"Mie",
"Shiga",
"Kyoto",
"Osaka",
"Hyogo",
"Nara",
"Wakayama",
"Tottori",
"Shimane",
"Okayama",
"Hiroshima",
"Yamaguchi",
"Tokushima",
"Kagawa",
"Ehime",
"Kochi",
"Fukuoka",
"Saga",
"Nagasaki",
"Kumamoto",
"Oita",
"Miyazaki",
"Kagoshima",
"Okinawa"]
kansen = pandas.read_csv("https://covid19.mhlw.go.jp/public/opendata/newly_confirmed_cases_daily.csv", encoding="utf-8")
jusyo = pandas.read_csv("https://covid19.mhlw.go.jp/public/opendata/severe_cases_daily.csv", encoding="utf-8")
for i in hyouji:
graph_kansen = pyplot
graph_kansen.title(i)
kansen2 = kansen[i]
date_kansen = [kansen["Date"][a][0:] for a in range(0,len(kansen["Date"]))]
date_kansen = pandas.to_datetime(date_kansen)
graph_kansen.xticks(rotation = 30)
graph_kansen.grid(which = "both", color = "blue", alpha = 0.8, linestyle = "dotted")
graph_kansen.plot(date_kansen, kansen2, color = "blue", marker=None)
graph_kansen.savefig(f"コロナ感染者 - {i}.png")
graph_kansen.clf()
graph_kansen.close()
###############################
graph_jusyo = pyplot
graph_jusyo.title(i)
jusyo2 = jusyo[i]
date_jusyo = [jusyo["Date"][a][0:] for a in range(0,len(jusyo["Date"]))]
date_jusyo = pandas.to_datetime(date_jusyo)
graph_jusyo.xticks(rotation = 30)
graph_jusyo.grid(which = "both", color = "blue", alpha = 0.8, linestyle = "dotted")
graph_jusyo.plot(date_jusyo, jusyo2, color = "red", marker=None)
graph_jusyo.savefig(f"コロナ重傷者 - {i}.png")
graph_jusyo.clf()
graph_jusyo.close()
###############################
graph_all = pyplot
graph_all.title(i)
graph_all.xticks(rotation = 30)
graph_all.grid(which = "both", color = "blue", alpha = 0.8, linestyle = "dotted")
graph_all.plot(date_kansen, kansen2, color = "blue", marker=None)
graph_all.plot(date_jusyo, jusyo2, color = "red", marker=None)
graph_all.savefig(f"コロナ総合 - {i}.png")
graph_all.clf()
graph_all.close()
print(i)
改善・追加した所
・CSVを最初に読ませる
・完全に一回一回
graph_jusyo.clf()
graph_jusyo.close()
↑でリセットしていること
・変数でそれぞれ詳しく分けた所
・見やすくした所
・補助線を追加
・日付をうまく表示させた
などが挙げられます
そして、このコードでは3つグラフを出します
・感染者数
・重症者数
・上2つを重ねたもの(※1)
実行結果
((※1)の結果を例に出しています)
前よりかなりいい感じに出せていると思います
まとめ
また、強化して行きたいです
コメントよろしくおねがいします
ご視聴ありがとうございました!