我对所有这些东西都很陌生,现在我正在学习Python。我知道基础知识,但现在我在使用新冠肺炎数据集时遇到了一个问题,在该数据集中,我想按大洲分组,以获得欧洲、亚洲等地区的总死亡率。当我查看可视化时,我只看到“其他”和太多的线条。希望你能帮助我,告诉我我做错了什么。现在,我认为问题出在for循环中。
我的代码:
import pandas as pd
import plotly.offline as pyo
import plotly.graph_objs as go
df = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")
df.set_index("continentExp", inplace=True)
del df["countryterritoryCode"], df["Cumulative_number_for_14_days_of_COVID-19_cases_per_100000"], df["geoId"], df["countriesAndTerritories"]
data = []
for name in df.index.unique():
trace = go.Scatter(
x = df["dateRep"],
y = df["deaths"],
name = name,
mode = "lines"
)
data.append(trace)
layout = go.Layout(
title = "Covid-19 Dashboard",
xaxis = {"title" : "Datum"},
yaxis = {"title" : "Tote"})
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)发布于 2020-07-15 02:24:03
在我看来,你遇到的问题是pandas而不是plotly。如果您正在按大陆查找每日死亡人数,则应使用groupby。
排列数据
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
df = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")
# better to have data as datetime
df["dateRep"] = df["dateRep"].astype("M8")
# the previous is equivalent to
# df["dateRep"] = pd.to_datetime(df["dateRep"])
# now you want to look for the daily deaths
# for every continent
grp = df.groupby(["continentExp","dateRep"])["deaths"]\
.sum().reset_index()使用plotly.express
fig = px.line(grp,
x="dateRep",
y="deaths",
color="continentExp",
labels={"deaths":"Tote",
"dateRep":"Datum"})
fig.update_layout(title="Covid-19 Dashboard",
title_x=0.5)

使用plotly.graph_objs
fig = go.Figure()
continents = grp["continentExp"].unique()
for continent in continents:
ts = grp[grp["continentExp"]==continent]
fig.add_trace(
go.Scatter(x=ts["dateRep"],
y=ts["deaths"],
name=continent))
fig.update_layout(title="Covid-19 Dashboard",
title_x=0.5,
xaxis={"title" : "Datum"},
yaxis={"title" : "Tote"})

https://stackoverflow.com/questions/62901187
复制相似问题