简体   繁体   中英

How to use plotly graph_objects scatter with date

starting from a known public data set which I copied to my own server.

The data set is here: https://www.kaggle.com/imdevskp/corona-virus-report/download

import pandas as pd
#df = pd.read_csv("http://g0mesp1res.dynip.sapo.pt/covid_19_clean_complete.csv", index_col=4, parse_dates=True)
df = pd.read_csv("http://g0mesp1res.dynip.sapo.pt/covid_19_clean_complete.csv")
df=df.drop(labels=None, axis=0, index=None, columns=['Province','Lat','Long'], level=None, inplace=False, errors='raise')
#print(df.head())
df['Date']=pd.to_datetime(df['Date'])
#print(df.head())
list_countries = ['Portugal','Brazil','Spain','Italy','Korea, South','Japan']
df= df[df['Country'].isin(list_countries)]
df_pt = df[df.Country == 'Portugal']
df_es = df[df.Country == 'Spain']
df_it = df[df.Country == 'Italy']
print(df_pt.head())
print(df_pt.tail())

I get what I expected

       Country       Date  Confirmed  Deaths  Recovered
59    Portugal 2020-01-22          0       0          0
345   Portugal 2020-01-23          0       0          0
631   Portugal 2020-01-24          0       0          0
917   Portugal 2020-01-25          0       0          0
1203  Portugal 2020-01-26          0       0          0
        Country       Date  Confirmed  Deaths  Recovered
15503  Portugal 2020-03-16        331       0          3
15789  Portugal 2020-03-17        448       1          3
16075  Portugal 2020-03-18        448       2          3
16361  Portugal 2020-03-19        785       3          3
16647  Portugal 2020-03-20       1020       6          5

However, when plotting, it seems that all data is in January!

import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=df.Date, y=df_pt.Confirmed, name='Portugal'))
fig.show()

plotly output graph :

在此处输入图片说明

What is missing?

Change the x axis from x=df.Date to x = df_pt.Date :

import plotly.graph_objects as go

fig = go.Figure(go.Scatter(x = df_pt.Date,
                           y = df_pt.Confirmed,
                           name='Portugal'))
fig.show()

and you get:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM