Skip to content

Commit

Permalink
Add helper for producing density scatter plots
Browse files Browse the repository at this point in the history
  • Loading branch information
hectornieto committed Mar 22, 2022
1 parent ec548b9 commit 88709fa
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion model_evaluation/double_collocation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import scipy.stats as st
from scipy.interpolate import interpn

def descriptive_stats(obs, pre):
"""
Expand Down Expand Up @@ -197,7 +198,6 @@ def rmse_stdev_decomposition(obs, pre):
obs, pre = remove_nans(obs, pre)

pre_hat = apply_calibration(obs, pre)

rmse_u = np.sqrt(np.mean((pre_hat - obs) ** 2))

return rmse_u
Expand Down Expand Up @@ -244,6 +244,7 @@ def scaling_factor(obs, pre):

return factor


def apply_calibration(obs, pre):
"""Apply the calibration coefficient between a variable and its reference
Expand Down Expand Up @@ -298,3 +299,27 @@ def remove_nans(obs, pre):
pre = pre[valid]
return obs, pre


def density_plot(x, y, ax, **scatter_kwargs):
"""
Creates a coloured density x-y scatter plot on a given subplot
Parameters
----------
x, y : array_like
Arrays of N elements of spatially-collocated observed and predicted systems.
ax : Object
Single matplotlib Axes object on which the density scatterplot will be
displayed
**scatter_kwargs :
Any additional keyword arguments passed to the pyplot.scatter call.
"""
x, y = remove_nans(x, y)
data, x_e, y_e = np.histogram2d(x, y, bins=30, density=True)
z = interpn((0.5 * (x_e[1:] + x_e[:-1]), 0.5 * (y_e[1:] + y_e[:-1])),
data,
np.array([x, y]).T,
method="splinef2d",
bounds_error=False)

ax.scatter(x,y, c=z, **scatter_kwargs)

0 comments on commit 88709fa

Please sign in to comment.