Source code for bca_tool_code.general_modules.create_figures

import pandas as pd
import matplotlib.pyplot as plt


[docs]class CreateFigures:
[docs] def __init__(self, df, units, destination, program): """ The CreateFigures class is used to generate figures (charts). Parameters: df: DataFrame; contains data to be charted.\n units: str; units for use on the y-axis of the created chart.\n destination: Path object; provides the path to which to save the created chart. \n program: str; the program identifier (i.e., 'CAP' or 'GHG') to include in the saved filename. """ self.df = df self.destination = destination / 'figures' self.destination.mkdir(exist_ok=True) self.units = units self.program = program
[docs] def line_chart_args_by_option(self, dr, alt_name, year_min, year_max, *args): """ This method generates a chart showing passed arguments under the given alternative. Parameters: dr: Numeric; the discount rate of the data to be charted.\n alt_name: str; the OptionName of the data to be charted.\n year_min: int; the minimum calendar year of data to be charted.\n year_max: int; the maximum calendar year of data to be charted.\n args: str(s); the data attributes to be charted. Returns: A single chart saved to the destination folder. """ data = self.df.loc[(self.df['DiscountRate'] == dr) & (self.df['optionName'] == alt_name) & (self.df['Series'] == 'AnnualValue') & ((self.df['yearID'] >= year_min) & (self.df['yearID'] <= year_max)), :] for arg in args: x, y = data['yearID'].astype(int), round(data[arg].astype(float)) plt.plot(x, y, label=f'{arg}') plt.title(f'{self.program}, Annual Costs, {alt_name}, {dr}DR') plt.xlabel('calendar year') plt.ylabel(f'{self.units}') plt.legend() plt.grid() plt.savefig(self.destination.joinpath(f'{self.program}_AnnualCosts_{alt_name}_{dr}DR.png')) plt.close() return
[docs] def line_chart_arg_by_options(self, dr, alt_names, year_min, year_max, arg): """ This method generates a chart showing the passed argument under each of the passed alternatives. Parameters: dr: Numeric; the discount rate of the data to be charted.\n alt_names: List; contains the OptionNames for which to chart data.\n year_min: int; the minimum calendar year of data to be charted.\n year_max: int; the maximum calendar year of data t be charted.\n arg: str; the single data attribute to be charted. Returns: A single chart saved to the destination folder. """ for alt_name in alt_names: data = self.df.loc[(self.df['DiscountRate'] == dr) & (self.df['optionName'] == alt_name) & (self.df['Series'] == 'AnnualValue') & ((self.df['yearID'] >= year_min) & (self.df['yearID'] <= year_max)), :] plt.plot((data.loc[data['optionName'] == alt_name, 'yearID']), (data.loc[data['optionName'] == alt_name, arg]), label=alt_name) plt.title(f'{self.program}, Annual Costs, {arg}, {dr}DR') plt.xlabel('calendar year') plt.ylabel(f'{self.units}') plt.legend(loc=5) plt.grid() plt.savefig(self.destination.joinpath(f'{self.program}_AnnualCosts_{arg}_{dr}DR.png')) plt.close() return
[docs] def create_figures(self, args): """ This method is called by tool_main and then controls the generation of charts by the CreateFigures class. Parameters: args: List; attributes to include in figures. Returns: Charts are saved to the path_for_save folder by the ChartFigures class and this method returns to tool_main. """ yearID_min = int(self.df['yearID'].min()) yearID_max = int(self.df['yearID'].max()) alt_names = [arg for arg in pd.Series(self.df['optionName'].unique()) if '_minus_' in arg] for alt_name in alt_names: self.line_chart_args_by_option(0, alt_name, yearID_min, yearID_max, *args) for arg in args: self.line_chart_arg_by_options(0, alt_names, yearID_min, yearID_max, arg) return