dashboard-temp / plot_utils.py
nataliegref's picture
translate text
1189f83
Raw
History Blame Contribute Delete
3.63 kB
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, label, split=False):
print('split', split)
if gdf.empty:
raise ValueError("GeoDataFrame is empty")
required_cols = [name1, name2]
for col in required_cols:
if col not in gdf.columns:
raise ValueError(f"Column '{col}' not found in gdf")
#Create a shared colormap and normalization
cmap = plt.colormaps[colorscheme]
norm = colors.Normalize(vmin=vmin, vmax=vmax)
# Create subplots
fig = plt.figure(figsize=(12, 6))
gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 0.05], wspace=0.3)
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1]), fig.add_subplot(gs[2]) ]
if split == False:
# First plot
gdf.plot(column=name1, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
else:
# Split data into three GeoDataFrames
# below_100 = gdf[gdf[split] <= 100]
above_capacity = gdf[gdf[split] > (100-gdf['%Construccion'])]
above_100 = gdf[gdf[split] > 100]
above_colors = {"capacity":"chocolate", "100":"firebrick"}
# Plot values ≤ 100 using colormap
gdf.plot(column=name1, cmap=colorscheme, ax=axes[0], vmin=vmin, vmax=vmax, marker='s', markersize=10)
# Plot values > capacity in orange
above_capacity.plot(color=above_colors['capacity'], ax=axes[0], label='> capacidad debido a la construcción', marker='x', markersize=6)
# Plot values > 100 in red
above_100.plot(color=above_colors['100'], ax=axes[0], label='> 100% cobertura arbórea', marker='x', markersize=6)
# Add legend manually for orange points
orange_patch = plt.Line2D([0], [0], marker='o', color='w', label='> capacidad debido a la construcción',
markerfacecolor=above_colors['capacity'], markersize=8)
# Add legend manually for red points
red_patch = plt.Line2D([0], [0], marker='o', color='w', label='> 100% cobertura arbórea',
markerfacecolor=above_colors['100'], markersize=8)
axes[0].legend(handles=[orange_patch, red_patch])
axes[0].set_title(title1)
axes[0].set_axis_off()
# Second plot (original)
gdf.plot(column=name2, cmap=cmap, norm=norm, ax=axes[1],marker='s', markersize=10)
axes[1].set_title(title2)
axes[1].set_axis_off()
# Shared colorbar
sm = cm.ScalarMappable(cmap=cmap, norm=norm)
sm._A = [] # Dummy array for the colormap
cbar = fig.colorbar(sm, cax=axes[2], orientation='vertical', fraction=0.03, pad=0.02)
cbar.set_label(label)
return fig
def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
# Second set of plots
cmap = plt.colormaps[colorscheme]
norm = colors.Normalize(vmin, vmax)
# Create subplots
fig = plt.figure(figsize=(12, 6))
gs = gridspec.GridSpec(1, 2, width_ratios=[1, 0.025], wspace=0.3)
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
gdf.plot(column=name, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
axes[0].set_title(title)
axes[0].set_axis_off()
# Add a colorbar
sm = cm.ScalarMappable(cmap=cmap, norm=norm)
sm._A = [] # Dummy data for the colorbar
cbar = fig.colorbar(sm, cax=axes[1], orientation='vertical', fraction=0.03, pad=0.02)
cbar.set_label(label)
return fig