Skip to content

Commit

Permalink
Fix shading (#123)
Browse files Browse the repository at this point in the history
* Updated environment, quicker setup

* Fixed bug + added docs for shading plot

* fix formatting

* Fix bug for spread defaults

* fix formatting

* Update whats-new.rst
  • Loading branch information
jbusecke authored Feb 15, 2022
1 parent 2f81f17 commit c61fa90
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 60 deletions.
24 changes: 13 additions & 11 deletions docs/environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name: xarrayutils-docs
channels:
- conda-forge
dependencies:
- python=3.8
- numpydoc
Expand All @@ -13,16 +15,16 @@ dependencies:
- dask
- numpy
- xarray
- scipy
- nc-time-axis
- astropy
- intake-esm
- gcsfs
- nbsphinx
- jupyter_client
- sphinxcontrib-srclinks
- pandoc
- recommonmark
- pip
- pip:
- scipy
- nc-time-axis
- astropy
- intake-esm
- gcsfs
- nbsphinx
- jupyter_client
- sphinx_pangeo_theme
- sphinxcontrib-srclinks
- pandoc
- recommonmark
- sphinx_pangeo_theme
163 changes: 123 additions & 40 deletions docs/plotting.ipynb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ New Features
~~~~~~~~~~~~
- `sing_agreement` now supports the option to not count nans along the given dimension (:pull:`118`). By `Julius Busecke <https://github.com/jbusecke>`_

Bugfixes
~~~~~~~~
- Fixed bug in `shaded_line_plot`, where std bounds were displayed incorrectly (:issue:`74`, :pull:`123`). By `Julius Busecke <https://github.com/jbusecke>`_

.. _whats-new.1.0.0:

v1.0.0 (2021/6/22)
Expand Down
20 changes: 14 additions & 6 deletions xarrayutils/plotting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tkinter import Y
import numpy as np
import xarray as xr
import warnings
Expand Down Expand Up @@ -233,7 +234,7 @@ def shaded_line_plot(
dim,
ax=None,
horizontal=True,
spreads=[1, 3],
spreads=None,
alphas=[0.25, 0.4],
spread_style="std",
line_kwargs=dict(),
Expand Down Expand Up @@ -317,8 +318,12 @@ def shaded_line_plot(
# define the line plot values
if spread_style == "std":
y = da.mean(dim)
if spreads is None:
spreads = [1, 3]
elif spread_style in ["quantile", "percentile"]:
y = da.quantile(0.5, dim)
if spreads is None:
spreads = [0.5, 0.8]
else:
raise ValueError(
f"Got unknown option ['{spread_style}'] for `spread_style`. Supported options are : ['std', 'quantile']"
Expand All @@ -340,16 +345,19 @@ def shaded_line_plot(
fill_defaults.update(fill_kwargs)
ff = []

for spread, alpha in zip(
(spreads), (alphas)
): # np.flip(this ensures that the shadings are drawn from outer to inner otherwise they blend too much into each other
spreads = list(np.flip(spreads))
alphas = list(np.flip(alphas))
# np.flip(this ensures that the shadings are drawn from outer to inner otherwise they blend too much into each other

for spread, alpha in zip(spreads, alphas):
f_kwargs = {k: v for k, v in fill_defaults.items()}
f_kwargs["alpha"] = alpha

if spread_style == "std":
y_std = da.std(dim) # i could probably precompute that.
y_lower = y - (y_std / (2 * spread))
y_upper = y + (y_std / (2 * spread))
y_spread = y_std * spread
y_lower = y - (y_spread / 2)
y_upper = y + (y_spread / 2)

elif spread_style in ["quantile", "percentile"]:
y_lower = da.quantile(0.5 - (spread / 2), dim)
Expand Down
3 changes: 2 additions & 1 deletion xarrayutils/test/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def test_shaded_line_plot(dim, horizontal, spreads, alphas):
ll, ff = shaded_line_plot(
da, dim, spreads=spreads, alphas=alphas, horizontal=horizontal
)

assert isinstance(ll[0], matplotlib.lines.Line2D)
assert len(ff) == min(len(spreads), len(alphas))

for f, a in zip(ff, alphas):
for f, a in zip(ff, np.flip(alphas)):
assert isinstance(f, matplotlib.collections.PolyCollection)
assert f.get_alpha() == a

Expand Down
2 changes: 1 addition & 1 deletion xarrayutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def xr_linregress(x, y, dim="time"):
cov = ((x - xmean) * (y - ymean)).sum(dim) / (n)
cor = cov / (xstd * ystd)

slope = cov / (xstd ** 2)
slope = cov / (xstd**2)
intercept = ymean - xmean * slope

df = n - 2
Expand Down
2 changes: 1 addition & 1 deletion xarrayutils/xmitgcm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def laplacian(grid, data):

def gradient_sq_amplitude(grid, data):
gradx, grady = gradient(grid, data, interpolate=True)
return gradx ** 2 + grady ** 2
return gradx**2 + grady**2


# Silly functions
Expand Down

0 comments on commit c61fa90

Please sign in to comment.