【问题标题】:How to use plotly graph_objects scatter with date如何使用带有日期的 plotly graph_objects scatter
【发布时间】:2020-03-23 11:20:26
【问题描述】:

从我复制到自己服务器的已知公共数据集开始。

数据集在这里: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())

我得到了我的期望

       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

但是,在绘图时,似乎所有数据都是一月份的!

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

绘制输出图:

缺少什么?

【问题讨论】:

    标签: python python-3.x pandas time-series plotly


    【解决方案1】:

    将 x 轴从 x=df.Date 更改为 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()
    

    你会得到:

    【讨论】:

    • 谢谢!我不得不多次阅读您的答案,然后将其粘贴到我的代码附近,直到我注意到我的 x 中有错字。不是 x = df_pt.Date,而是 x = df.Date。所以,为 x 使用不同的 df!
    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 2020-09-05
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2022-09-30
    • 2020-11-22
    • 2020-01-09
    相关资源
    最近更新 更多