# Create a list to hold the results for each variable and month results = []
# List of variables to analyze
variables = ['prp', 'tmin', 'tmax', 'flow']
# Create a plot for each monsoon month and each variable
for month in monsoon_months: for var in variables: monthly_data = df1[df1['Month'].dt.month == month][var].to_numpy() monthly_data = monthly_data[~np.isnan(monthly_data)] # Remove NaN values if present
# Check if there is data to fit
if len(monthly_data) > 0: # Fit the data to a distribution df.fit_transform(monthly_data)
# Get summary of the fitted distributions
summary = df.summary best_fit = summary.iloc[0] # Get the best fitting distribution
# Collect results for the month and variable
results.append({ 'Month': df1['Month'].dt.month_name()[month - 1], # Month name 'Variable': var, 'Best Distribution': best_fit['name'], 'Parameters': best_fit['params'] })
# Plot the results
plt.figure() df.plot() plt.title(f"Distribution Fit for {df1['Month'].dt.month_name()[month - 1]} - {var}") # Month and variable name plt.xlabel(f"{var} values") plt.ylabel("Density") plt.tight_layout() # Improve layout
# Save the figure
plt.savefig(f"Distribution_Fit_{df1['Month'].dt.month_name()[month - 1]}_{var}.png") # Save figure as PNG plt.close() # Close the figure to avoid displaying it
else: print(f"No data available for {df1['Month'].dt.month_name()[month - 1]} in {var}")
# Create a DataFrame from the results
results_df = pd.DataFrame(results)
# Print the summary DataFrame
print("\nSummary of Best Fitting Distributions for Monsoon Months:") print(results_df)
# Export the results to a CSV file
results_df.to_csv('monsoon_distribution_fits_all_variables.csv', index=False) print("\nResults have been exported to 'monsoon_distribution_fits_all_variables.csv'.")
# Optionally, show all plots (if you wish to see them)