-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid19Graphics.py
139 lines (118 loc) · 6.17 KB
/
covid19Graphics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Example showing how to plot a single state using a downloaded csv
# Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy
# In computer programming, pandas is a software library written for the Python programming language for data manipulation and analysis
from matplotlib import pyplot as plt
import pandas as pd
from matplotlib.pyplot import figure
# https://www.kaggle.com/fireballbyedimyrnmom/us-counties-covid-19-dataset
# https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv
df = pd.read_csv(
'https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv'
)
while True:
ch1 = raw_input("1- Plot State\n2- All State\ne- Exit\n")
if ch1 == "1":
city = raw_input("Please, enter a city (Colorado, New York, Kansas etc..) in USA: ")
status = raw_input("Please, select status to see deaths/cases: ")
# Create a new data frame of a single state.
df_city = df[ df['state'] == str(city) ].copy()
# Make sure pandas realizes our date column is a date.
df_city['date'] = pd.to_datetime(df_city['date'])
# Sum up all the cases for city by date.
# Use diff method to compair to previous day to get new cases.
series_city_sum = df_city.groupby('date')[str(status)].sum()
series_city_diff = series_city_sum.diff()
plt.plot(series_city_diff.index, series_city_diff.values, label=city)
# x ve y eksenine neye ait olduklarını yazdırmak için
plt.xlabel("Date")
plt.ylabel(str(status))
# Grafik title
plt.title(str(city)+" "+str(status)+" per day.")
plt.legend()
plt.show()
elif ch1 == "2":
ch2 = raw_input("1- Line Plot\n2- Pie Chart\n")
if ch2 == "1":
# Increase default chart size.
figure(num=None, figsize=(16, 12))
# THIS PULLS IN LIVE DATA.
url = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
df = pd.read_csv(url)
unique_states = df['state'].unique()
status = raw_input("Please, select status to see deaths/cases: ")
# Add base style and print out style choices.
plt.style.use('seaborn-colorblind')
print(plt.style.available)
# Get last date to see which states have the most cases currently
last_date = df['date'].max()
df_last_date = df[ df['date'] == last_date]
series_last_date = df_last_date.groupby('state')[str(status)].sum()
# kaç tane eyaletin grafikte gözükmesini istiyorsam burasaya default bir değer giriyorum.
series_last_date = series_last_date.nlargest(10)
# Remove left and right plot lines.
ax = plt.subplot()
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
# Bir başlangıç tarihi belirledim grafikte daha net gözükmeleri için
# The chat is flat before 3/15/2020 so lets limit it to after that date.
date_after = pd.Timestamp("03/15/2020")
for state in series_last_date.index:
df_state = df[ df['state'] == state].copy()
df_state['date'] = pd.to_datetime(df_state['date'])
df_state = df_state[ df_state['date'] > date_after]
series_state = df_state.groupby('date')[str(status)].sum()
series_state = series_state.diff()
series_state.index = series_state.index.strftime('%b %d')
plt.plot(series_state.index, series_state.values, label=state)
plt.xlabel("Date")
plt.ylabel(str(status))
plt.title('Top 10 Covid-19 States')
plt.grid(True)
# Plot custom range for our Y axis
plt.yticks(range(0, 12001, 1000))
# rotate our dates.
plt.xticks(rotation=60)
plt.legend()
plt.show()
if ch2 == "2":
status = raw_input("Please, select status to see deaths/cases: ")
df = pd.read_csv(
'https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv'
)
unique_states = df['state'].unique()
plt.style.use("seaborn-talk")
# Get last date to see which states have the most cases currently
last_date = df['date'].max()
df_last_date = df[ df['date'] == last_date]
series_last_date = df_last_date.groupby('state')[str(status)].sum().sort_values(ascending=False)
print(series_last_date)
labels = []
values = []
# kaç tane ülkenin pie chartta olmasını istiyorsak o kadar count var,
state_count = 6
other_total = 0
for state in series_last_date.index:
if state_count > 0:
labels.append(state)
values.append(series_last_date[state])
state_count -= 1
else:
other_total += series_last_date[state]
# bir de geri kalanlar için other kısmı
labels.append("Other")
values.append(other_total)
wedge_dict = {
'edgecolor': 'white',
'linewidth': 2
}
# pie chartımız da max değerlere sahip 6 ülke var, ülke sayısını artırıp azalarak düzenlenmeli.
# 0.1 şekilde ayrık dilimi elde etmemizi sağladı.
explode = (0, 0.1, 0, 0, 0, 0, 0)
plt.pie(values, labels=labels, explode=explode, autopct='%1.1f%%', wedgeprops=wedge_dict)
plt.show()
elif ch1 == "e":
print("Exiting...")
break
else:
print("Invalid command")
continue