【问题标题】:How to plot a single row of time series data in python如何在python中绘制单行时间序列数据
【发布时间】:2021-05-14 08:44:16
【问题描述】:

我正在尝试使用 python/pandas 和 matplotlib 绘制某些时间序列数据。我能够从原始 github 页面中提取如下数据。我想知道是否有办法绘制数据框中的所有列。为检索数据编写的代码如下。

import pandas as pd
import requests
import io
url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
download = requests.get(url).content
df = pd.read_csv(io.StringIO(download.decode('utf-8')))
filt = df['Country/Region'] == 'India'
i_rec = df[filt]
dt_filt = i_rec[df.columns.drop(['Province/State','Country/Region','Lat','Long'])]
print(dt_filt)

Screenshot

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    您可以转置时间序列来绘制它。

    plt.plot(dt_filt.transpose())
    

    当然,您可能希望根据自己的喜好更改轴标签和标记。

    这是一个带有旋转和缩小轴标签的示例:

    fig, ax = plt.subplots()
    
    # plot the (transposed) time series data
    ax.plot(dt_filt.transpose())
    
    # show only every 25th label (otherwise they'd overlap a lot)
    every_nth = 25
    for n, label in enumerate(ax.xaxis.get_ticklabels()):
        if n % every_nth != 0:
            label.set_visible(False)
    
    # rotate the labels s.t. more can fit
    plt.xticks(rotation=90)
    
    # show the plot
    plt.show()
    

    减少标签数量的代码改编自this StackOverflow answer

    结果将如下所示:

    【讨论】:

      【解决方案2】:

      你可以这样做

      dt_filt.T.plot()
      

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 1970-01-01
        • 2022-12-11
        • 2016-09-13
        • 1970-01-01
        • 2020-05-23
        • 2018-11-17
        • 2020-04-09
        相关资源
        最近更新 更多