-1

I am trying to plot certain timeseries data using python/pandas and matplotlib. I was able to extract the data as below from a raw github page. I wanted to know if there is a way to plot all the columns in a data frame. Code written for retrieving the data as below.

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

2 Answers2

1

You can transpose the time series to plot it.

plt.plot(dt_filt.transpose())

Of course you might want to alter the axis labels and markers to your preferences.

Here's an example with rotated and reduced axis labels:

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()

The code to reduce the number of labels was adapted from this StackOverflow answer.

The result wil look something like the following:

enter image description here

fravolt
  • 2,565
  • 1
  • 4
  • 19
0

you can do this

dt_filt.T.plot()
peko
  • 31
  • 2