Created
October 31, 2021 15:56
-
-
Save gegen07/3b4ef5ab64375ff4228ffb3aced442e5 to your computer and use it in GitHub Desktop.
plot results function used in JOSS figure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from spopt.locate.coverage import MCLP | |
mclp = MCLP.from_cost_matrix( | |
cost_matrix, | |
ai, | |
max_coverage=service_dist, | |
p_facilities=4, | |
) | |
mclp = mclp.solve(pulp.GLPK(msg=False)) | |
mclp.facility_client_array() | |
plot_results(mclp, facility_points_gdf.to_crs({"init": 'epsg:7131'}), demand_points_gdf.to_crs({"init": 'epsg:7131'}), facility_points_gdf.shape[0], "MCLP", save=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib.patches import Patch | |
import matplotlib.lines as mlines | |
from matplotlib_scalebar.scalebar import ScaleBar | |
dv_colors = [ | |
"darkcyan", | |
"mediumseagreen", | |
"cyan", | |
"darkslategray", | |
"lightskyblue", | |
"limegreen", | |
"darkgoldenrod", | |
"peachpuff", | |
"coral", | |
"mediumvioletred", | |
"blueviolet", | |
"fuchsia", | |
"thistle", | |
"lavender", | |
"saddlebrown", | |
] | |
def plot_results(model, facility_points_gdf, demand_points_gdf, facility_count, title, save=False): | |
arr_points = [] | |
fac_sites = [] | |
for i in range(facility_count): | |
if model.fac2cli[i]: | |
geom = demand_points_gdf.iloc[model.fac2cli[i]]['geometry'] | |
arr_points.append(geom) | |
fac_sites.append(i) | |
fig, ax = plt.subplots(figsize=(12, 12)) | |
legend_elements = [] | |
tract.to_crs({"init": 'epsg:7131'}).plot(ax=ax, alpha=1, color='tan', zorder=1, edgecolor='black') | |
legend_elements.append(mlines.Line2D( | |
[], | |
[], | |
color='tan', | |
label='tract', | |
)) | |
facility_points_gdf.plot(ax=ax, color='brown', marker="*", markersize=80, zorder=2) | |
legend_elements.append(mlines.Line2D( | |
[], | |
[], | |
color='brown', | |
marker="*", | |
linewidth=0, | |
label=f'facility sites ($n$={facility_count})' | |
)) | |
demand_points_gdf.plot(ax=ax, edgecolor='k', color='white', zorder=2) | |
legend_elements.append(mlines.Line2D( | |
[], | |
[], | |
color='white', | |
marker="o", | |
markeredgecolor='k', | |
linewidth=0, | |
label=f'clients not covered' | |
)) | |
for i in range(len(arr_points)): | |
gdf = geopandas.GeoDataFrame(arr_points[i]) | |
label = f"points covered by {facility_points_gdf.iloc[[fac_sites[i]]]['NAME'].squeeze()}" | |
legend_elements.append(Patch(facecolor=dv_colors[i], edgecolor="k", label=label)) | |
gdf.plot(ax=ax, zorder=3, alpha=0.7, edgecolor="k", color=dv_colors[i], label=label) | |
facility_points_gdf.iloc[[fac_sites[i]]].plot(ax=ax, | |
marker="*", | |
markersize=200 * 3.0, | |
alpha=0.8, | |
zorder=4, | |
edgecolor="k", | |
facecolor=dv_colors[i]) | |
legend_elements.append(mlines.Line2D( | |
[], | |
[], | |
color=dv_colors[i], | |
marker="*", | |
ms=20 / 2, | |
markeredgecolor="k", | |
linewidth=0, | |
alpha=0.8, | |
label=f"{facility_points_gdf.iloc[[fac_sites[i]]]['NAME'].squeeze()}", | |
)) | |
x, y, arrow_length = 0.925, 0.15, 0.1 | |
plt.annotate('N', xy=(x, y), xycoords='axes fraction', xytext=(x, y-arrow_length), | |
arrowprops=dict(facecolor='black', width=5, headwidth=10), | |
ha='center', va='center', fontsize=20) | |
plt.gca().add_artist(ScaleBar(1)) | |
plt.title(title, fontweight="bold") | |
plt.legend(handles = legend_elements, loc='upper left', bbox_to_anchor=(1.05, 1)) | |
if save == True: | |
plt.savefig(title, bbox_inches='tight') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment