Spaces:
Sleeping
Sleeping
Upload residuals.py
Browse files- helpers/residuals.py +32 -0
helpers/residuals.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import seaborn as sns
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import geopandas as gp
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
def get_residual_plot(country_name, gdf):
|
| 8 |
+
fig, ax = plt.subplots(1,1,figsize=(10,6))
|
| 9 |
+
if country_name is None:
|
| 10 |
+
ax.text(0.5, 0.5, "Click on the map to see the IWI time-series here.",
|
| 11 |
+
horizontalalignment='center', verticalalignment='center',
|
| 12 |
+
transform=ax.transAxes, fontsize=14)
|
| 13 |
+
|
| 14 |
+
elif gdf['country'].str.contains(country_name).any():
|
| 15 |
+
gdf = gdf[gdf['country'] == country_name]
|
| 16 |
+
gdf['date'] = pd.to_datetime(gdf['date'])
|
| 17 |
+
modified_xticks = gdf['date'].dt.to_period('M')
|
| 18 |
+
ticks_unique = np.unique(modified_xticks)
|
| 19 |
+
sns.boxplot(x=modified_xticks, y=gdf['residual'], ax=ax)
|
| 20 |
+
ax.set_xlabel('date')
|
| 21 |
+
ax.set_xticks(range(len(ticks_unique)))
|
| 22 |
+
ax.set_xticklabels(labels = ticks_unique, rotation=45)
|
| 23 |
+
ax.set_ylabel('iwi_value')
|
| 24 |
+
ax.set_title(f'IWI Residual Plot For {country_name}')
|
| 25 |
+
ax.grid(True, linestyle='--', alpha=0.7)
|
| 26 |
+
plt.tight_layout()
|
| 27 |
+
return fig
|
| 28 |
+
|
| 29 |
+
else:
|
| 30 |
+
ax.text(0.5, 0.5, "No country data available. Please select another country.",
|
| 31 |
+
horizontalalignment='center', verticalalignment='center',
|
| 32 |
+
transform=ax.transAxes, fontsize=14)
|