def plot_bar_plot(data, title, ylabel, filename=None, formatter='{:.1f}'): """ Helper function used to compute bar plots. :param y_values: :param title: :param xlabel: :param filename: :return: """ y_vals = data.values y_pos = np.arange(len(y_vals)) x_ticks_labels = data.index.values matplotlib = import_matplotlib( ) # this need to be imported before matplotlib plt. import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(PP_TEXT_WIDTH_IN, PP_TEXT_HEIGHT_IN)) rect = ax.bar(y_pos, y_vals, align='center', alpha=0.5) ax.set_ylabel(ylabel) autolabel(rect, ax, False, formatter) # add annotations to plot plt.xticks(y_pos, x_ticks_labels, rotation=45) plt.title(title) plt.subplots_adjust(hspace=0.5, top=0.9, bottom=0.2, left=0.07, right=0.97) #plt.legend() if filename: plt.savefig(os.path.join(base, save_dir, '{}.png'.format(filename))) return
def plot_2grouped_bar_plot(list_y_values, labels, x_ticks_labels, title, ylabel, filename=None): """ Helper function used to compute bar plots. TODO add list_labels, :param list_y_values: :param title: :param xlabel: :param filename: :return: """ y_pos = np.arange(len(list_y_values[0])) width = 0.35 matplotlib = import_matplotlib( ) # this need to be imported before matplotlib plt. import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(PP_TEXT_WIDTH_IN, PP_TEXT_HEIGHT_IN)) #for i, y_values in enumerate(list_y_values): rect = ax.bar(y_pos - width / 2, list_y_values[0], width, align='center', alpha=0.5, label='{}'.format(labels[0])) autolabel(rect, ax) # add annotations to plot rect2 = ax.bar(y_pos + width / 2, list_y_values[1], width, align='center', alpha=0.5, label='{}'.format(labels[1])) autolabel(rect2, ax) # add annotations to plot ax.set_ylabel(ylabel) plt.xticks(y_pos, x_ticks_labels, rotation=45) plt.title(title) plt.legend(loc='lower left') plt.subplots_adjust(hspace=0.5, top=0.9, bottom=0.2, left=0.07, right=0.97) if filename: plt.savefig(os.path.join(base, save_dir, '{}.png'.format(filename))) return
import xarray as xr import seaborn as sns import glob import os from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, AutoMinorLocator) from sclouds.helpers import (path_input, path_stats_results, VARIABLES, UNITS, LONGNAME, STATISTICS, LONGNAME_STATISTICS) from sclouds.io.utils import get_xarray_dataset_for_period from sclouds.plot.helpers import (TEXT_WIDTH_IN, TEXT_HEIGHT_IN, path_python_figures, import_matplotlib, cmap_contour_plot, levels_contourplot, color_maps, add_ticks) mat = import_matplotlib() #for mye import matplotlib.pyplot as plt import matplotlib as mpl fig, axes = plt.subplots(nrows = 3, ncols = 1, sharex=True, sharey=False) fig.set_size_inches(w = TEXT_WIDTH_IN, h = TEXT_HEIGHT_IN-2) ################## The AR model folders = ['AR-B-5'] degree = 'L5' files = glob.glob('/home/hanna/EX3_Results_AR/AR-B-5/*performance*AR*L5*') name = '-'.join(files[0].split('_')[-3].split('-5-')) data = xr.open_mfdataset(files, combine='by_coords') data['latitude'] = data.latitude.values.astype(float)
import os import glob import numpy as np import xarray as xr import pandas as pd from sclouds.plot.helpers import import_matplotlib mat = import_matplotlib() import matplotlib.pyplot as plt def read_global_statistics_to_dataframe(): """ Read computed statistics to dataframe. """ read_dir = '/home/hanna/lagrings/ERA5_monthly/' save_dir = '/home/hanna/lagrings/ERA5_stats/results/' save_dir = '/home/hanna/lagrings/results/stats/' STATS = ['mean', 'std', 'min', 'max', 'median', 'mad'] # 'median', VALID_VARS = ['r', 'q', 't2m', 'sp', 'tcc'] VALID_FILTERS = ['coast', 'sea', 'land', 'artefact', 'all'] results = {} for f in VALID_FILTERS: results[f] = {} for var in VALID_VARS: files = glob.glob( os.path.join(save_dir, '*global*{}*{}*.nc'.format(var, f))) if len(files) != 1:
def save_test_ar(test_fil): import os import glob import numpy as np import xarray as xr from sclouds.plot.helpers import TEXT_HEIGHT_IN, TEXT_WIDTH_IN, import_matplotlib mat = import_matplotlib() import matplotlib.pyplot as plt from sclouds.ml.regression.AR_model_loader import AR_model_loader m1 = AR_model_loader().load_model_to_xarray(test_fil) save_dir = '/home/hanna/MS-thesis/python_figs/test/{}'.format( test_fil.split('.nc')[0].split('/')[-1]) if not os.path.isdir(save_dir): try: os.makedirs(save_dir, exist_ok=True) #print("Directory '%s' created successfully" %save_dir) except OSError as error: #print("Directory '%s' can not be created") print(error) per = ['mse', 'ase', 'r2'] fig, axes = plt.subplots(len(per), 1, figsize=(TEXT_WIDTH_IN, TEXT_HEIGHT_IN)) for i, p in enumerate(per): m1[p].plot(ax=axes[i]) fig.suptitle('Sig:{}, Trans:{}, bias:{}, order:{}'.format( m1['sigmoid'].values, m1['transform'].values, m1['bias'].values, m1['order'].values)) plt.savefig(os.path.join(save_dir, 'performance.png')) per = ['num_train_samples', 'num_test_samples'] fig, axes = plt.subplots(len(per), 1, figsize=(TEXT_WIDTH_IN, TEXT_HEIGHT_IN)) for i, p in enumerate(per): m1[p].plot(ax=axes[i]) fig.suptitle('Sig:{}, Trans:{}, bias:{}, order:{}'.format( m1['sigmoid'].values, m1['transform'].values, m1['bias'].values, m1['order'].values)) plt.savefig(os.path.join(save_dir, 'num_samples.png')) va = [] for i in range(1, +1): va.append('W{}'.format(i)) try: print(m1['Wt2m']) var = ['Wt2m', 'Wsp', 'Wr', 'Wq'] except KeyError: #print('failed ...') var = [] order = m1['order'].values per = va + var if m1['bias'].values: per += ['bias'] fig, axes = plt.subplots(len(per) + order, 1, figsize=(TEXT_WIDTH_IN, TEXT_HEIGHT_IN)) for i, p in enumerate(per): m1[p].plot(ax=axes[i]) fig.suptitle('Sig:{}, Trans:{}, bias:{}, order:{}'.format( m1['sigmoid'].values, m1['transform'].values, m1['bias'].values, m1['order'].values)) plt.savefig(os.path.join(save_dir, 'weights.png')) return