基于 Jupyter Notebook 和Plotly的交互式COVID-19实时追踪可视化系统
3.“散点图”图表
renamed_columns_map = {
"Country/Region": "country",
"Province/State": "location",
"Lat": "latitude",
"Long": "longitude"
}
confirmed_cases_df = (
pd.read_csv(confirmed_cases_url)
.rename(columns=renamed_columns_map)
.rename(columns=reformat_dates)
.fillna(method="bfill", axis=1)
)
deaths_df = (
pd.read_csv(deaths_url)
.rename(columns=renamed_columns_map)
.rename(columns=reformat_dates)
.fillna(method="bfill", axis=1)
)
display(confirmed_cases_df.head())
display(deaths_df.head())
fig = go.Figure()
geo_data_cols = ["country", "location", "latitude", "longitude"]
geo_data_df = confirmed_cases_df[geo_data_cols]
dates_list = (
confirmed_cases_df.filter(regex=r"(d{2}/d{2}/d{4})", axis=1)
.columns
.to_list()
)
#创建日期的映射- >数据帧,其中每个DF持有的情况下,每个国家的死亡日常计数
cases_by_date = {}
for date in dates_list:
#得到的所有情况pd.Series当天
confirmed_cases_day_df = (
confirmed_cases_df.filter(like=date, axis=1)
.rename(columns=lambda col: "confirmed_cases")
.astype("uint32")
)
#得到所有死亡人数的pd.Series当天
deaths_day_df = (
deaths_df.filter(like=date, axis=1)
.rename(columns=lambda col: "deaths")
.astype("uint32")
)
cases_df = confirmed_cases_day_df.join(deaths_day_df) #合并案例和死亡dfs
cases_df = geo_data_df.join(cases_df) #添加地理数据
cases_df = cases_df[cases_df["confirmed_cases"] > 0] #删除没有案例的任何行
cases_by_date[date] = cases_df
#每个数据帧看起来是这样的:
cases_by_date[dates_list[-1]].head()
#生成每一天的数据
fig.data = []
for date, df in cases_by_date.items():
df["confirmed_cases_norm"] = np.log1p(df["confirmed_cases"])
df["text"] = (
date
+ "<br>"
+ df["country"]
+ "<br>"
+ df["location"]
+ "<br>Confirmed cases: "
+ df["confirmed_cases"].astype(str)
+ "<br>Deaths: "
+ df["deaths"].astype(str)
)
fig.add_trace(
go.Scattergeo(
name="",
lat=df["latitude"],
lon=df["longitude"],
visible=False,
hovertemplate=df["text"],
showlegend=False,
marker={
"size": df["confirmed_cases_norm"] * 100,
"color": "red",
"opacity": 0.75,
"sizemode": "area",
},
)
)
#整理出注释和滑块步骤
annotation_text_template = "<b>Worldwide Totals</b>"
"<br>{date}<br><br>"
"Confirmed cases: {confirmed_cases:,d}<br>"
"Deaths: {deaths:,d}<br>"
"Mortality rate: {mortality_rate:.1%}"
annotation_dict = {
"x": 0.03,
"y": 0.35,
"width": 175,
"height": 110,
"showarrow": False,
"text": "",
"valign": "middle",
"visible": False,
"bordercolor": "black",
}
steps = []
for i, data in enumerate(fig.data):
step = {
"method": "update",
"args": [
{"visible": [False] * len(fig.data)},
{"annotations": [dict(annotation_dict) for _ in range(len(fig.data))]},
],
"label": dates_list[i],
}
#将第i个跟踪和注释框切换到可见
step["args"][0]["visible"][i] = True
step["args"][1]["annotations"][i]["visible"] = True
df = cases_by_date[dates_list[i]]
confirmed_cases = df["confirmed_cases"].sum()
deaths = df["deaths"].sum()
mortality_rate = deaths / confirmed_cases
step["args"][1]["annotations"][i]["text"] = annotation_text_template.format(
date=dates_list[i],
confirmed_cases=confirmed_cases,
deaths=deaths,
mortality_rate=mortality_rate,
)
steps.append(step)
sliders = [
{
"active": 0,
"currentvalue": {"prefix": "Date: "},
"steps": steps,
"len": 0.9,
"x": 0.05,
}
]
first_annotation_dict = {**annotation_dict}
first_annotation_dict.update(
{
"visible": True,
"text": annotation_text_template.format(
date="10/01/2020", confirmed_cases=44, deaths=1, mortality_rate=0.0227
),
}
)
fig.layout.title = {"text": "Covid-19 Global Case Tracker", "x": 0.5}
fig.update_layout(
height=650,
margin={"t": 50, "b": 20, "l": 20, "r": 20},
annotations=[go.layout.Annotation(**first_annotation_dict)],
sliders=sliders,
)
fig.data[0].visible = True #设置第一个数据点可见
fig
4.参考:
-
[Michael](https://mfreeborn.github.io/)
-
[Jupyter Notebook](https://jupyter.org/)
-
[Plotly](https://pypi.org/project/plotly/)