520 - 7 LineGraphs & TimeSeries : Why does the for looping result in different value plotting, in both versions function calling approach?

Screen Link:
https://app.dataquest.io/c/95/m/520/line-graphs-and-time-series/7/types-of-growth

My Code:

**VERSION 1 EXTERNAL CALL CHAIN** 
def plot_list(country_names):
    for country in country_names:
            plot_cumulative_cases(country)

def plot_cumulative_cases(country_name):
    cntry = who_time_series[who_time_series['Country'] == country_name]
    plt.plot(cntry['Date_reported'], cntry['Cumulative_cases'])
    plt.title('{}: Cumulative Reported Cases'.format(country_name))
    plt.xlabel('Date')
    plt.ylabel('Number of Cases')
    plt.show()

plot_list(['Brazil','Iceland','Argentina'])
brazil = 'exponential'
argentina = 'exponential'
iceland = 'logarithmic'





**VERSION 2 FUNCTION DEFINITION**
def plot_cumulative_cases(country_name):
    for country in country_name:
        cntry = who_time_series[who_time_series['Country'] == country]
        plt.plot(cntry['Date_reported'], cntry['Cumulative_cases'])
        plt.title('{}: Cumulative Reported Cases'.format(country))
        plt.xlabel('Date')
        plt.ylabel('Number of Cases')
        plt.show()

plot_cumulative_cases(['Brazil','Iceland','Argentina'])    
#plt.plot(who_time_series[who_time_series['Country']=='India']['Date_reported'],who_time_series[who_time_series['Country']=='India']['Cumulative_cases'])
brazil = 'exponential'
argentina = 'exponential'
iceland = 'logarithmic'

What I expected to happen:
Work fine as it is just a same function being called exactly same number of times for each country

What actually happened:

error : 
One of your variables doesn't seem to have the correct value. Please re-check the instructions and your code.
Your 1st plot doesn't match what we expected.
(actually none do)

Screenshots for reference (both versions same error) (re-pasted with better screenshot)

If I copy/paste your code, it is accepted by the system (both versions) and I get the plots that are shown on the right hand side of your screenshots.

My best guess for the difference in plots is that your data is somehow different. Try running:

import pandas as pd
who_time_series = pd.read_csv('WHO_time_series_reduced.csv')
who_time_series['Date_reported'] = pd.to_datetime(who_time_series['Date_reported'])

before plotting to make sure your data is clean.

1 Like