-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
107 move plotting functions to plotspy (#124)
* Aggregating RWI by region and updating existing data if it exists * Fixed RWI aggregation and updating * Minor changes * GADM+CHIRPS data being unpacked and processed for different levels and timeframes * Changed get chirps_rainfall_data() input to be a partial date * Completed process_chirps_rainfall_data() * Completed and testing geospatial/chirps-rainfall and meteorological/chirps-rainfall * Updated README * Fixing failing tests * Fixing failing tests * Fixing failing tests * Fixing failing tests * Created plot_heatmap() * Plotting chirps-rainfall and terraclimate * Added tests * Fixing failing tests * Fixing failing tests * Fixing failing tests * Refactoring RWI * Added plotting to RWI * Minor change * WIP * Changed the plotting functions to produce either macro level or micro level borders * Added tests * Fixing failing tests * Fixing failing tests
- Loading branch information
1 parent
5d98224
commit 725e1c5
Showing
5 changed files
with
400 additions
and
137 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""Plot data.""" | ||
from pathlib import Path | ||
import logging | ||
import re | ||
|
||
from matplotlib import pyplot as plt | ||
import geopandas as gpd | ||
import numpy as np | ||
|
||
from .util import output_path | ||
|
||
|
||
def plot_heatmap(data, title, colourbar_label, path): | ||
"""Create a heat map.""" | ||
data[data == 0] = np.nan | ||
plt.imshow(data, cmap='coolwarm', origin='upper') | ||
plt.colorbar(label=colourbar_label) | ||
plt.title(title) | ||
# Make the plot title file-system safe | ||
title = re.sub(r'[<>:"/\\|?*]', '_', title) | ||
title = title.strip() | ||
# Export | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
logging.info('exporting:%s', path) | ||
plt.savefig(path) | ||
plt.close() | ||
|
||
|
||
def plot_gadm_micro_heatmap( | ||
source, data, gdf, pdate, title, colourbar_label, region, extent | ||
): | ||
"""Create a heat map with GADM region overlaid.""" | ||
geometry = region.geometry | ||
min_lon, min_lat, max_lon, max_lat = geometry.bounds | ||
_, ax = plt.subplots() | ||
im = ax.imshow(data, cmap='coolwarm', origin='upper', extent=extent) | ||
# Add the geographical borders | ||
gdf.plot(ax=ax, color='none', edgecolor='gray') | ||
gpd.GeoDataFrame([region]).plot(ax=ax, color='none', edgecolor='k') | ||
# Add colour bar | ||
plt.colorbar(im, ax=ax, label=colourbar_label) | ||
# Titles and axes | ||
ax.set_title(title) | ||
ax.set_xlim(min_lon, max_lon) | ||
ax.set_ylim(min_lat, max_lat) | ||
ax.set_xlabel('Longitude') | ||
ax.set_ylabel('Latitude') | ||
# Make the plot title file-system safe | ||
title = re.sub(r'[<>:"/\\|?*]', '_', title) | ||
title = title.strip() | ||
# Export | ||
path = Path( | ||
output_path(source), str(pdate).replace('-', '/'), title + '.png' | ||
) | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
logging.info('exporting:%s', path) | ||
plt.savefig(path) | ||
plt.close() | ||
|
||
|
||
def plot_gadm_macro_heatmap( | ||
data, origin, extent, limits, gdf, zorder, title, colourbar_label, path | ||
): | ||
"""Create a heat map with a macro GADM region overlaid.""" | ||
_, ax = plt.subplots() | ||
gdf.boundary.plot(ax=ax, edgecolor='k', linewidth=0.5, zorder=zorder) | ||
im = ax.imshow(data, cmap='coolwarm', origin=origin, extent=extent) | ||
# Add colour bar | ||
plt.colorbar(im, ax=ax, label=colourbar_label) | ||
# Titles and axes | ||
ax.set_title(title) | ||
ax.set_xlim(limits[0], limits[2]) | ||
ax.set_ylim(limits[1], limits[3]) | ||
ax.set_xlabel('Longitude') | ||
ax.set_ylabel('Latitude') | ||
# Make the plot title file-system safe | ||
title = re.sub(r'[<>:"/\\|?*]', '_', title) | ||
title = title.strip() | ||
# Export | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
logging.info('exporting:%s', path) | ||
plt.savefig(path) | ||
plt.close() |
Oops, something went wrong.