2

I am writing a script which can be used to plot the country wise covid time-series data. It is working fine when I plot a single country but The scale at Y-axis is in appropriately printed. Plot which I am getting The Problem is after printing the maximum value for one country the y axis is extrapolated with smaller values to plot the data points of subsequent countries. The code for my script is as follows

import requests
from contextlib import closing
import csv
import matplotlib.pyplot as plt
url = "https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv"

def prepareCountryWiseData(country):
    countryWise = {}
    with closing(requests.get(url, stream=True)) as r:
        f = (line.decode('utf-8') for line in r.iter_lines())
        reader = csv.reader(f, delimiter=',', quotechar='"')
        active = []
        recovered = []
        dates = []
        for row in reader:    
             if row[1] == country:
                     dates.append(row[0])
                     active.append(row[2])
                     recovered.append(row[3])
        return (dates, active, recovered)

def plotCountryWiseData(countryList):
    plotable = []
    for country in countryList:
            dates,active,recovered = (prepareCountryWiseData(country))
            plt.plot(active)                
    plt.ylabel('active_cases')
    plt.legend(countryList)
    plt.show()
    plotCountryWiseData(['India','US','Italy'])
2
  • Can you use the pandas module for this project? Commented May 3, 2020 at 17:04
  • 1
    I have not used pandas . Now I have managed to find a hack of this problem. I have rounded the values and it matplotlib has handled it automatically. But I am unaware if some technical solution exists for it. If you know It using pandas please share your thoughts It will be helpful. Commented May 3, 2020 at 17:07

1 Answer 1

1

If you can use the pandas module your job would be much easier:

import pandas as pd, matplotlib.pyplot as plt

url = "https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv"
df = pd.read_csv(url)

fig,ax = plt.subplots()

for k,g in df[df['Country'].isin(['India','US','Italy'])].groupby('Country'):
    ax = g.plot(ax=ax,kind='line',x='Date',y='Confirmed',label=k) 

plt.gcf().suptitle('Active Cases')
plt.show()

Result:
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.