def plot_indicator ( indicators , title = None , xlim = None , ylim = None , xspace = None , loc = 0 , loc2 = 0 , drop_eu = False , filename = None ): lines = [ '-' , '--' ] line_styles = [] fig , ax = plt . subplots () indicators = indicators if isinstance ( indicators , list ) else [ indicators ] for line , ( name , indicator ) in zip ( lines , indicators ): ls , = plt . plot ( np . nan , linestyle = line , color = '#999999' ) line_styles . append ([ ls , name ]) df_ind = df [( df . IndicatorCode == indicator )] group = df_ind . groupby ([ 'CountryName' ]) for country , values in group : country_values = values . groupby ( 'Year' ) . mean () if country == 'avg. EU' : if drop_eu : continue ax . plot ( country_values , label = country , linestyle = '--' , color = '#666666' , linewidth = 1 , zorder = 1 ) elif country_values . shape [ 0 ] > 1 : ax . plot ( country_values , label = country , linestyle = line , color = colors [ country ], linewidth = 2.5 ) if line == lines [ 0 ]: legend = plt . legend ( loc = loc ) ax . set_xlim ( xlim ) ax . set_ylim ( ylim ) if xlim and xspace : ax . set_xticks ( np . arange ( xlim [ 0 ], xlim [ 1 ] + 1 , xspace )) plt . tight_layout () fig . subplots_adjust ( top = 0.94 ) if title : ax . set_title ( title ) else : ax . set_title ( df_ind . IndicatorName . values [ 0 ]) if len ( indicators ) > 1 : plt . legend ( * zip ( * line_styles ), loc = loc2 ) ax . add_artist ( legend ) if filename : global i i += 1 plt . savefig ( './figures/wdi/wdi_ {:02d} _ {} .png' . format ( i , filename ))