update draw.py
This commit is contained in:
		
							
								
								
									
										115
									
								
								draw.py
									
									
									
									
									
								
							
							
						
						
									
										115
									
								
								draw.py
									
									
									
									
									
								
							| @@ -1,5 +1,6 @@ | |||||||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||||||
| import matplotlib.ticker as ticker | import matplotlib.ticker as ticker | ||||||
|  | from matplotlib.ticker import FuncFormatter | ||||||
| import numpy as np | import numpy as np | ||||||
| import pandas as pd | import pandas as pd | ||||||
| import os | import os | ||||||
| @@ -7,7 +8,6 @@ import seaborn as sns | |||||||
| import json | import json | ||||||
| from matplotlib.colors import LinearSegmentedColormap | from matplotlib.colors import LinearSegmentedColormap | ||||||
|  |  | ||||||
|  |  | ||||||
| def read_data(file_name: str): | def read_data(file_name: str): | ||||||
|     with open(file_name, 'r') as f: |     with open(file_name, 'r') as f: | ||||||
|         data = json.load(f) |         data = json.load(f) | ||||||
| @@ -15,25 +15,43 @@ def read_data(file_name: str): | |||||||
|         for subkey, subvalue in value.items(): |         for subkey, subvalue in value.items(): | ||||||
|             data[key][subkey] = float(subvalue) |             data[key][subkey] = float(subvalue) | ||||||
|     df = pd.DataFrame.from_dict(data, orient='index') |     df = pd.DataFrame.from_dict(data, orient='index') | ||||||
|  |     df = df.T | ||||||
|     df.index = pd.to_numeric(df.index) |     df.index = pd.to_numeric(df.index) | ||||||
|     df.columns = pd.to_numeric(df.columns) |     df.columns = pd.to_numeric(df.columns) | ||||||
|     return df |     return df | ||||||
|  |  | ||||||
| def draw_results(results, filename, title, annot_benefit=False, figure_size=(10, 10)): | def draw_results(results, filename, title_benefit, annot_benefit=False, figure_size=(10, 10)): | ||||||
|     df= results |     df=results | ||||||
|     df = df.astype(float) |     df = df.astype(float) | ||||||
|     df.index = df.index / 1000 |     df.index = df.index / 1000 | ||||||
|  |     df.index = df.index.map(int) | ||||||
|     df.columns = df.columns / 1000 |     df.columns = df.columns / 1000 | ||||||
|  |     df.columns = df.columns.map(int) | ||||||
|     min_value = df.min().min() |     min_value = df.min().min() | ||||||
|     max_value = df.max().max() |     max_value = df.max().max() | ||||||
|     max_scale = max(abs(min_value/1000), abs(max_value/1000)) |     max_scale = max(abs(min_value/1000), abs(max_value/1000)) | ||||||
|     plt.figure(figsize=figure_size) |  | ||||||
|  |     df[df.columns[-1] + 1] = df.iloc[:, -1]  | ||||||
|  |     new_Data = pd.DataFrame(index=[df.index[-1] + 1], columns=df.columns) | ||||||
|  |     for i in df.columns: | ||||||
|  |         new_Data[i] = df[i].iloc[-1] | ||||||
|  |     # print(new_Data) | ||||||
|  |     df = pd.concat([df, new_Data]) | ||||||
|  |  | ||||||
|  |     X, Y = np.meshgrid(np.arange(df.shape[1]), np.arange(df.shape[0])) | ||||||
|  |  | ||||||
|  |     def fmt(x,pos): | ||||||
|  |         return '{:.0f}'.format(x/1000) | ||||||
|  |  | ||||||
|     cmap = sns.color_palette("coolwarm", as_cmap=True) |     cmap = sns.color_palette("coolwarm", as_cmap=True) | ||||||
|  |     plt.figure(figsize=figure_size) | ||||||
|     ax = sns.heatmap(df/1000, fmt=".1f", cmap=cmap, vmin=-max_scale, vmax=max_scale, annot=annot_benefit) |     ax = sns.heatmap(df/1000, fmt=".1f", cmap=cmap, vmin=-max_scale, vmax=max_scale, annot=annot_benefit) | ||||||
|     ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f"{x:.2f}")) |     CS = ax.contour(X, Y, df,  colors='black', alpha=0.5) | ||||||
|     # ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.1f')) |     ax.clabel(CS, inline=True, fontsize=10, fmt=FuncFormatter(fmt)) | ||||||
|     plt.title(title) |     plt.title(title_benefit) | ||||||
|     plt.gca().invert_yaxis() |     plt.gca().invert_yaxis() | ||||||
|  |     plt.xlim(0, df.shape[1] - 1) | ||||||
|  |     plt.ylim(0, df.shape[0] - 1) | ||||||
|     plt.xlabel('ESS Capacity (MWh)') |     plt.xlabel('ESS Capacity (MWh)') | ||||||
|     plt.ylabel('PV Capacity (MW)') |     plt.ylabel('PV Capacity (MW)') | ||||||
|     plt.savefig(filename) |     plt.savefig(filename) | ||||||
| @@ -41,43 +59,84 @@ def draw_results(results, filename, title, annot_benefit=False, figure_size=(10, | |||||||
| def draw_cost(costs, filename, title_cost, annot_cost=False, figure_size=(10, 10)): | def draw_cost(costs, filename, title_cost, annot_cost=False, figure_size=(10, 10)): | ||||||
|     df = costs |     df = costs | ||||||
|     df = df.astype(int) |     df = df.astype(int) | ||||||
|     print(df.index) |  | ||||||
|     df.index = df.index / 1000 |     df.index = df.index / 1000 | ||||||
|     print(df.columns) |     df.index = df.index.map(int) | ||||||
|     df.columns = df.columns / 1000 |     df.columns = df.columns / 1000 | ||||||
|  |     df.columns = df.columns.map(int) | ||||||
|  |  | ||||||
|  |     df[df.columns[-1] + 1] = df.iloc[:, -1]  | ||||||
|  |     new_Data = pd.DataFrame(index=[df.index[-1] + 1], columns=df.columns) | ||||||
|  |     for i in df.columns: | ||||||
|  |         new_Data[i] = df[i].iloc[-1] | ||||||
|  |     # print(new_Data) | ||||||
|  |     df = pd.concat([df, new_Data]) | ||||||
|  |     X, Y = np.meshgrid(np.arange(df.shape[1]), np.arange(df.shape[0])) | ||||||
|  |  | ||||||
|  |     def fmt(x, pos): | ||||||
|  |         return '{:.0f}'.format(x / 1000000) | ||||||
|  |  | ||||||
|     plt.figure(figsize=figure_size) |     plt.figure(figsize=figure_size) | ||||||
|     sns.heatmap(df/1000000,  fmt=".1f", cmap='viridis', annot=annot_cost) |     ax = sns.heatmap(df/1000000,  fmt=".1f", cmap='viridis', annot=annot_cost) | ||||||
|  |     CS = ax.contour(X, Y, df,  colors='black', alpha=0.5) | ||||||
|  |     ax.clabel(CS, inline=True, fontsize=10, fmt=FuncFormatter(fmt)) | ||||||
|     plt.title(title_cost) |     plt.title(title_cost) | ||||||
|     plt.gca().invert_yaxis() |     plt.gca().invert_yaxis() | ||||||
|  |     plt.xlim(0, df.shape[1] - 1) | ||||||
|  |     plt.ylim(0, df.shape[0] - 1) | ||||||
|     plt.xlabel('ESS Capacity (MWh)') |     plt.xlabel('ESS Capacity (MWh)') | ||||||
|     plt.ylabel('PV Capacity (MW)') |     plt.ylabel('PV Capacity (MW)') | ||||||
|     print(filename) |  | ||||||
|     plt.savefig(filename) |     plt.savefig(filename) | ||||||
|  |  | ||||||
|  |  | ||||||
| def draw_overload(overload_cnt, filename, title_unmet, annot_unmet=False, figure_size=(10, 10)): | def draw_overload(overload_cnt, filename, title_unmet, annot_unmet=False, figure_size=(10, 10)): | ||||||
|     df = overload_cnt |     df = overload_cnt | ||||||
|     df = df.astype(int) |     df = (4 * 24 * 365 - df) / (4 * 24 * 365) | ||||||
|  |     df = df.astype(float) | ||||||
|     df.index = df.index / 1000 |     df.index = df.index / 1000 | ||||||
|  |     df.index = df.index.map(int) | ||||||
|     df.columns = df.columns / 1000 |     df.columns = df.columns / 1000 | ||||||
|  |     df.columns = df.columns.map(int) | ||||||
|  |     print('unmet index') | ||||||
|  |     print(df.index) | ||||||
|  |     print('unmet columns') | ||||||
|  |     print(df.columns) | ||||||
|     min_value = df.min().min() |     min_value = df.min().min() | ||||||
|     max_value = df.max().max() |     max_value = df.max().max() | ||||||
|     max_scale = max(abs(min_value/1000), abs(max_value/1000)) |     # max_scale = max(abs(min_value/1000), abs(max_value/1000)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     df[df.columns[-1] + 1] = df.iloc[:, -1]  | ||||||
|  |     new_Data = pd.DataFrame(index=[df.index[-1] + 1], columns=df.columns) | ||||||
|  |     for i in df.columns: | ||||||
|  |         new_Data[i] = df[i].iloc[-1] | ||||||
|  |     # print(new_Data) | ||||||
|  |     df = pd.concat([df, new_Data]) | ||||||
|  |  | ||||||
|  |  | ||||||
|     plt.figure(figsize=figure_size) |     plt.figure(figsize=figure_size) | ||||||
|     cmap = LinearSegmentedColormap.from_list("", ["white", "blue"]) |     cmap = LinearSegmentedColormap.from_list("", ["white", "blue"]) | ||||||
|     ax = sns.heatmap(df/(4*24*365), fmt=".00%", cmap=cmap, vmin=0, vmax=1, annot=annot_unmet) |     ax = sns.heatmap(df, fmt=".00%", cmap=cmap, vmin=0, vmax=1, annot=annot_unmet) | ||||||
|  |  | ||||||
|     cbar = ax.collections[0].colorbar |     cbar = ax.collections[0].colorbar | ||||||
|     cbar.set_ticks([0, 0.25, 0.5, 0.75, 1]) |     cbar.set_ticks([0, 0.25, 0.5, 0.75, 1]) | ||||||
|     cbar.set_ticklabels(['0%', '25%', '50%', '75%', '100%']) |     cbar.set_ticklabels(['0%', '25%', '50%', '75%', '100%']) | ||||||
|     cbar.ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: f'{x:.0%}')) |     cbar.ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: f'{x:.0%}')) | ||||||
|  |     X, Y = np.meshgrid(np.arange(df.shape[1]), np.arange(df.shape[0])) | ||||||
|  |  | ||||||
|  |     def fmt(x, pos): | ||||||
|  |         return '{:.0f}%'.format(x * 100) | ||||||
|  |     CS = ax.contour(X, Y, df,  colors='black', alpha=0.5) | ||||||
|  |  | ||||||
|  |     ax.clabel(CS, inline=True, fontsize=10, fmt=FuncFormatter(fmt)) | ||||||
|  |  | ||||||
|  |     plt.xlim(0, df.shape[1] - 1) | ||||||
|  |     plt.ylim(0, df.shape[0] - 1) | ||||||
|     plt.title(title_unmet) |     plt.title(title_unmet) | ||||||
|     plt.gca().invert_yaxis() |  | ||||||
|     plt.xlabel('ESS Capacity (MWh)') |     plt.xlabel('ESS Capacity (MWh)') | ||||||
|     plt.ylabel('PV Capacity (MW)') |     plt.ylabel('PV Capacity (MW)') | ||||||
|     plt.savefig(filename) |     plt.savefig(filename) | ||||||
|  |  | ||||||
|  |  | ||||||
| directory = 'data/' | directory = 'data/' | ||||||
|  |  | ||||||
| file_list = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] | file_list = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] | ||||||
| @@ -99,19 +158,19 @@ overload_dfs = [read_data(directory + '-'.join(f)) for f in overload_files] | |||||||
| results_dfs = [read_data(directory + '-'.join(f)) for f in results_files] | results_dfs = [read_data(directory + '-'.join(f)) for f in results_files] | ||||||
|  |  | ||||||
| for costs_df, overload_df, results_df in zip(costs_dfs, overload_dfs, results_dfs): | for costs_df, overload_df, results_df in zip(costs_dfs, overload_dfs, results_dfs): | ||||||
|     # print(costs_df.index) |  | ||||||
|     # print(pd.to_numeric(costs_df.columns)) |     draw_cost(costs_df,  | ||||||
|     # costs_df.index = pd.to_numeric(costs_df.columns ) |               f'plots/costs-{int(costs_df.columns[-1])}-pv-{int(costs_df.index[-1])}.png',  | ||||||
|     # costs_df.columns = pd.to_numeric(costs_df.index) |               f'Costs for ESS-{int(costs_df.columns[-1])}-pv-{int(costs_df.index[-1])}MWh', annot_cost=False) | ||||||
|     print(costs_df) |  | ||||||
|     draw_cost(costs_df, f'plots/costs-{int(costs_df.columns[-1])}.png', f'Costs for PV-{int(costs_df.columns[-1])}MW ESS-{int(costs_df.index[-1])}MWh', annot_cost=True) |     draw_overload(overload_df,  | ||||||
|     # overload_df.index = pd.to_numeric(overload_df.columns, errors='coerce') |                   f'plots/overload-ess-{overload_df.columns[-1]}-pv-{overload_df.index[-1]}.png',  | ||||||
|     # overload_df.columns = pd.to_numeric(overload_df.columns, errors='coerce') |                   f'Overload for ess-{overload_df.columns[-1]}MW pv-{overload_df.index[-1]}MWh',  | ||||||
|     print(overload_df) |                   annot_unmet=False) | ||||||
|     # draw_overload(overload_df, f'plots/overload-{overload_df.columns[-1]}', f'Overload for PV-{overload_df.columns[-1]}MW ESS-{overload_df.index[-1]}MWh', annot_unmet=True) |  | ||||||
|     # results_df.index = pd.to_numeric(results_df.columns, errors='coerce') |     draw_results(results_df,  | ||||||
|     # results_df.columns = pd.to_numeric(results_df.columns, errors='coerce') |                  f'plots/results-{results_df.columns[-1]}-pv-{results_df.index[-1]}.png',  | ||||||
|     # draw_results(results_df, f'plots/results-{results_df.columns[-1]}', f'Results for PV-{results_df.columns[-1]}MW ESS-{results_df.index[-1]}MWh', annot_benefit=True) |                  f'Results for ess-{results_df.columns[-1]}MW pv-{results_df.index[-1]}MWh', annot_benefit=False) | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user