#!/usr/bin/env python # coding: utf-8 # In[2]: import xarray as xr import numpy as np import matplotlib.pyplot as plt import glob import pandas as pd # In[31]: #Define file path to open data #path1 = str(input('File_path_1: ')) #path2 = str(input('File_path_2: ')) path1 = r'/work/users/hmo/remoclic/Phuong/PHD/BC_2023/Tan/NC/' path2 = r'/work/users/hmo/remoclic/Phuong/PHD/BC_2023/Tan/NC/' #Open and sort the list of file in the previous path #list_of_files = sorted(glob.glob(path1), key = lambda x: x.split('_')[-1]) #list_of_files_2 = sorted(glob.glob(path2), key = lambda x: x.split('_')[-1]) #list_of_files = ["Geo_ORIG_CNRM.nc","Geo_ORIG_CSIRO.nc","Geo_ORIG_ECEA.nc","Geo_ORIG_GFDL.nc","Geo_ORIG_HadG.nc","Geo_ORIG_MPI.nc","Geo_ORIG_ENS.nc"] #list_of_files_2 = ["Geo_QM_CNRM.nc","Geo_QM_CSIRO.nc","Geo_QM_ECEA.nc","Geo_QM_GFDL.nc","Geo_QM_HadG.nc","Geo_QM_MPI.nc","Geo_QM_ENS.nc"] list_of_files = ["CNRM","CSIRO","ECEA","GFDL","HadG","MPI","ENS"] print(list_of_files) #print(list_of_files_2) Xlab=[] Del1=[] Del2=[] for i in range(0,len(list_of_files)): del1=1 + i*3 del2=2 + i*3 xlab=i+1 Del1.append(del1) Del2.append(del2) Xlab.append(xlab) list1=[] list2=[] for file in list_of_files: list1.append(path1+'Geo_ORIG_'+file+'.nc') list2.append(path1+'Geo_QM_'+file+'.nc') # In[18]: #Write a function to collect "geo" data from the file path, store in a dataset with the columns name is the name of the file def create_dataframe(file_paths, var): # Create an empty dictionary to store the data of geo data_dict = {} # Iterate over the file paths for file_path in file_paths: # Open the netcdf dataset with xarray library dataset = xr.open_dataset(file_path) # Extract the data of variable var_data = dataset[var][0] # Get the file name without the directory and extension file_name = file_path.split('\\')[-1].split('.')[0] # Store the data in the dictionary with the file name as the key data_dict[file_name] = var_data.values # Create the DataFrame from the dictionary df = pd.DataFrame(data_dict) return df #Output data storing the variables needed to plot #df = create_dataframe(list_of_files, var = 'geo') #df1 = create_dataframe(list_of_files_2, var = 'geo') df = create_dataframe(list1, var = 'geo') df1 = create_dataframe(list2, var = 'geo') # In[28]: # Create a box plot for each column in the DataFrame fig, ax = plt.subplots(figsize=(10, 6)) #Set the labels labels = df.columns #Plot the boxplot for dataframe c = "red" bplot1 = ax.boxplot(df, positions = Del1, notch = True, vert = True, patch_artist = True, # labels = list_of_files, # labels = Del1, ## labels = Xlab, boxprops=dict(facecolor=c, color=c)) c1 ="blue" bplot2 = ax.boxplot(df1, positions = Del2, notch = True, vert = True, patch_artist = True, # labels = list_of_files, ## labels = Xlab, boxprops=dict(facecolor=c1, color=c1)) ax.set_xticklabels(list_of_files) #Set the title for figure by the last character of columns name ax.set_title('Box plot of '+ str(df.columns[0]).split('_')[0] + ' data') # Display the plot #plt.show() #Save figure plt.savefig('test.pdf')