#!/usr/bin/env python # coding: utf-8 # In[1]: import xarray as xr import numpy as np import matplotlib.pyplot as plt import glob import pandas as pd # In[2]: #Define file path to open data path = r'/work/users/hmo/remoclic/Phuong/PHD/BC_2023/Tan/NC/Geo_*.nc' #Open and sort the list of file in the previous path list_of_files = sorted(glob.glob(path), key = lambda x: x.split('_')[-1]) #list_of_files # In[3]: #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_geo_dataframe(file_paths): # 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 "geo" variable geo_data = dataset['geo'][0] # Get the file name without the directory and extension file_name = file_path.split('\\')[-1].split('.')[0] # Store the "geo" data in the dictionary with the file name as the key data_dict[file_name] = geo_data.values # Create the DataFrame from the dictionary df = pd.DataFrame(data_dict) return df #Output data storing the geo values geo_df = create_geo_dataframe(list_of_files) geo_df # In[4]: #print(geo_df.columns) # In[7]: # Create a box plot for each column in the DataFrame fig, ax = plt.subplots(figsize=(10, 6)) #Set the labels labels = geo_df.columns #Plot the boxplot for dataframe bplot1 = ax.boxplot(geo_df, notch = True, vert = True, patch_artist = True, labels = labels) #Set the title for figure ax.set_title('Box plot of '+ str(geo_df.columns[0]).split('_')[0] + ' data') colors = [] for column in geo_df.columns: if 'ORIG' in column: color = 'pink' elif 'QM' in column: color = 'lightgreen' else: print("Error: This file doesn't contain ORIG or QM.") continue colors.append(color) # fill box plots with colors for patch, color in zip(bplot1['boxes'], colors): patch.set_facecolor(color) # Display the plot plt.show() #Save figure #plt.savefig('E:/thay_Tan/chi_phuong/Box_plot_of_Geo.png')