Пример #1
0
 def _plot(self, ax, x, Y):
     if len(x) == 0:
         print("No data to plot")
         return
         
     count = 0 # This keeps track of "unused" colors
     for idx, y in enumerate(Y):
         # Split the data into subsets (if needed)
         X_subset, Y_subset = splitNA(x, y)
       
         # Get the color and linestyle
         if len(Y_subset) == 0: # nothing to plot
             count += 1
         color, linetype = self.line(idx-count)
         
         # Plot the data
         plotWithNA(X_subset, Y_subset, ax, y.name, color, linetype)
     return
Пример #2
0
    def show(self, countryList, name, normalise_by=None, in_percent=True):
        """
        Plot the values of an indicator for a given country.
        
        Country can be either the three letter country code or the full country
        name. The indicator name can either be the full description or the code 
        used in the World Bank data. It returns the years and the corresponding
        values of the indicator.
        The data can be normalised by the an indicator specified in the
        normalise_by variable. If in_percent is True, the indicator is taken
        to be in percent of the normalise_by indicator. Instead percent values,
        the true value will be shown.
        
        Input:
          country (list):      The country names. Can be list containing multiple
                               countries (either three letter code or full name) or
                               single string.
          
          name (str):          World Bank Indicator
          
          normalise_by (str):  World Bank Indicator by which the data should
                               be normalised.
        
          in_percent (bool):   If True, the indicator will be treated as being
                               in percent of the normalise_by column.
        
        Output:
          x (list):       List of pandas Series objects containing the year values
          
          y (list):       List of pandas Series objects containing the indicator values 
          
          c (list):       List of country names (three letter code). The order
                          is matching the order of the x and y values.
                          
        """
        # Get the data
        X, Y, C = self.indicator(countryList, name)
        
        # Get the normalisation indicator
        if normalise_by is not None:
            X_norm, Y_norm, C_norm = self.indicator(countryList, normalise_by)
            assert( np.all([ c==c_norm for c,c_norm in zip(C, C_norm) ]) ) # sanity check
            assert( np.all([ x==x_norm for x,x_norm in zip(X, X_norm) ]) )
            if in_percent:
                Y = [ y*y_norm for y,y_norm in zip(Y,Y_norm) ]
            else:
                Y = [ y/y_norm for y,y_norm in zip(Y,Y_norm) ]
                

        # Was the input understood?
        if len(X) == 0:
            print("Nothing to plot")
            return
        
        # Assemble the figure title
        if name[2] == '.': # This is a pretty ugly comparison.. but hopefully
                           # will do.
            name = self.WorldBankMapper(name) # Map to indicator code
        
        if normalise_by is not None:
            if normalise_by[2] == '.':
                normalise_by = self.WorldBankMapper(normalise_by) # Map to indicator code
            if in_percent:
                name = name + " [conv. to real unit]"
            else:
                name = name + " [normalised by %s]" %normalise_by

        # Generate the figure            
        fig = plt.figure(figsize=self.singleFigureSize, dpi=self.dpi)
        ax  = fig.add_subplot(111)
        ax.set_title(name, y=1.04)
        ax.set_xlabel("Year", fontsize=self.axisLabelSize)
        ax.set_ylabel("Indicator", fontsize=self.axisLabelSize)
        ax.set_xlim( [int(min(X[0]))-1, int(max(X[0]))+1] )

        for idx, (x, y, c) in enumerate(zip(X,Y,C)):
            # If there are missing values we do not want to plot it continuesly
            # but indicate the regions with the missing values.
            
                X_subset, Y_subset = splitNA(x, y)
                plotWithNA(X_subset, Y_subset, ax, c, self.line(idx)[0])
        
        # Add the legend, remove duplicates. Since the plot is compsed piecewise,
        # there will be multiple instances of each line for the legend.
        _handles, _labels = ax.get_legend_handles_labels()
        labels  = list(set(_labels))
        handles = [ _handles[_labels.index(item)] for item in labels ]
        
        ax.legend(handles, labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        
        return