AIGDL_Visualization / residuals.py
chrisli124's picture
Upload 20 files
6570159 verified
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import geopandas as gp
import numpy as np
def get_residual_plot(country_name, gdf):
fig, ax = plt.subplots(1,1,figsize=(10,6))
if country_name is None:
ax.text(0.5, 0.5, "Click on the map to see the IWI time-series here.",
horizontalalignment='center', verticalalignment='center',
transform=ax.transAxes, fontsize=14)
elif gdf['country'].str.contains(country_name).any():
gdf = gdf[gdf['country'] == country_name]
gdf['date'] = pd.to_datetime(gdf['date'])
modified_xticks = gdf['date'].dt.to_period('M')
ticks_unique = np.unique(modified_xticks)
sns.boxplot(x=modified_xticks, y=gdf['residual'], ax=ax)
ax.set_xlabel('date')
ax.set_xticks(range(len(ticks_unique)))
ax.set_xticklabels(labels = ticks_unique, rotation=45)
ax.set_ylabel('iwi_value')
ax.set_title(f'IWI Residual Plot For {country_name}')
ax.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
return fig
else:
ax.text(0.5, 0.5, "No country data available. Please select another country.",
horizontalalignment='center', verticalalignment='center',
transform=ax.transAxes, fontsize=14)