Skip to content

Commit

Permalink
Merge pull request #129 from xylar/add-matplotlib-style
Browse files Browse the repository at this point in the history
Use common matplotlib style sheet in all ocean plots
  • Loading branch information
xylar authored Oct 5, 2023
2 parents c737933 + 977dad9 commit 1037179
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 296 deletions.
3 changes: 2 additions & 1 deletion docs/developers_guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ seaice/api
.. autosummary::
:toctree: generated/
use_mplstyle
plot_horiz_field
globe.plot_global
plot_global_field
```

### yaml
Expand Down
26 changes: 19 additions & 7 deletions docs/developers_guide/framework/visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ While developers can write their own visualization scripts associated with
individual tasks, the following shared visualization routines are
provided in `polaris.viz`:

(dev-visualization-style)=

## common matplotlib style

The function {py:func}`polaris.viz.use_mplstyle()` loads a common
[matplotlib style sheet](https://matplotlib.org/stable/users/explain/customizing.html#customizing-with-style-sheets)
that can be used to make font sizes and other plotting options more consistent
across Polaris. The plotting functions described below make use of this common
style. Custom plotting should call {py:func}`polaris.viz.use_mplstyle()`
before creating a `matplotlib` figure.

(dev-visualization-planar)=

## horizontal fields from planar meshes
Expand Down Expand Up @@ -46,7 +57,7 @@ plot_horiz_field(config, ds, ds_mesh, 'normalVelocity',

## global lat/lon plots from spherical meshes

You can use {py:func}`polaris.viz.globe.plot_global()` to plot a field on
You can use {py:func}`polaris.viz.plot_global_field()` to plot a field on
a regular lon-lat mesh, perhaps after remapping from an MPAS mesh using
{py:class}`polaris.remap.MappingFileStep`.

Expand All @@ -55,7 +66,7 @@ a regular lon-lat mesh, perhaps after remapping from an MPAS mesh using
:width: 500 px
```

The `plot_land` parameter to {py:func}`polaris.viz.globe.plot_global()` is used
The `plot_land` parameter to {py:func}`polaris.viz.plot_global_field()` is used
to enable or disable continents overlain on top of the data:

```{image} images/cosine_bell_final_land.png
Expand All @@ -69,17 +80,18 @@ import cmocean # noqa: F401
import xarray as xr

from polaris import Step
from polaris.viz.globe import plot_global
from polaris.viz import plot_global_field

class Viz(Step):
def run(self):
ds = xr.open_dataset('initial_state.nc')
ds = ds[['tracer1']].isel(Time=0, nVertLevels=0)

plot_global(ds.lon.values, ds.lat.values, ds.tracer1.values,
out_filename='init.png', config=self.config,
colormap_section='cosine_bell_viz',
title='Tracer at init', plot_land=False)
plot_global_field(
ds.lon.values, ds.lat.values, ds.tracer1.values,
out_filename='init.png', config=self.config,
colormap_section='cosine_bell_viz',
title='Tracer at init', plot_land=False)
```

The `colormap_section` of the config file must contain config options for
Expand Down
2 changes: 2 additions & 0 deletions polaris/ocean/convergence/spherical/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from polaris import Step
from polaris.ocean.resolution import resolution_to_subdir
from polaris.viz import use_mplstyle


class SphericalConvergenceAnalysis(Step):
Expand Down Expand Up @@ -203,6 +204,7 @@ def plot_convergence(self, variable_name, title, zidx):
order1 = 0.5 * error_array[-1] * (res_array / res_array[-1])
order2 = 0.5 * error_array[-1] * (res_array / res_array[-1])**2

use_mplstyle()
fig = plt.figure()

error_dict = {'l2': 'L2 norm', 'inf': 'L-infinity norm'}
Expand Down
3 changes: 2 additions & 1 deletion polaris/ocean/tasks/baroclinic_channel/rpe/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from polaris import Step
from polaris.ocean.rpe import compute_rpe
from polaris.viz import plot_horiz_field
from polaris.viz import plot_horiz_field, use_mplstyle


class Analysis(Step):
Expand Down Expand Up @@ -79,6 +79,7 @@ def run(self):
ds = xr.open_dataset(f'output_nu_{nus[0]:g}.nc', decode_times=False)
times = ds.daysSinceStartOfSim.values

use_mplstyle()
fig = plt.figure()
for i in range(sim_count):
rpe_norm = np.divide((rpe[i, :] - rpe[i, 0]), rpe[i, 0])
Expand Down
24 changes: 12 additions & 12 deletions polaris/ocean/tasks/cosine_bell/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from polaris import Step
from polaris.remap import MappingFileStep
from polaris.viz.globe import plot_global
from polaris.viz import plot_global_field


class VizMap(MappingFileStep):
Expand Down Expand Up @@ -133,20 +133,20 @@ def run(self):
ds_init = remapper.remap(ds_init)
ds_init.to_netcdf('remapped_init.nc')

plot_global(ds_init.lon.values, ds_init.lat.values,
ds_init.tracer1.values,
out_filename='init.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer at init', plot_land=False)
plot_global_field(
ds_init.lon.values, ds_init.lat.values, ds_init.tracer1.values,
out_filename='init.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer at init', plot_land=False)

ds_out = xr.open_dataset('output.nc')
ds_out = ds_out[['tracer1', ]].isel(Time=-1, nVertLevels=0)
ds_out = remapper.remap(ds_out)
ds_out.to_netcdf('remapped_final.nc')

plot_global(ds_init.lon.values, ds_init.lat.values,
ds_out.tracer1.values,
out_filename='final.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer after {run_duration:g} days',
plot_land=False)
plot_global_field(
ds_init.lon.values, ds_init.lat.values, ds_out.tracer1.values,
out_filename='final.png', config=config,
colormap_section='cosine_bell_viz',
title=f'{mesh_name} tracer after {run_duration:g} days',
plot_land=False)
3 changes: 2 additions & 1 deletion polaris/ocean/tasks/inertial_gravity_wave/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from polaris.ocean.tasks.inertial_gravity_wave.exact_solution import (
ExactSolution,
)
from polaris.viz import plot_horiz_field
from polaris.viz import plot_horiz_field, use_mplstyle


class Viz(Step):
Expand Down Expand Up @@ -68,6 +68,7 @@ def run(self):
section = config['inertial_gravity_wave']
eta0 = section.getfloat('ssh_amplitude')

use_mplstyle()
fig, axes = plt.subplots(nrows=nres, ncols=3, figsize=(12, 2 * nres))
rmse = []
error_range = None
Expand Down
3 changes: 2 additions & 1 deletion polaris/ocean/tasks/manufactured_solution/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from polaris.ocean.tasks.manufactured_solution.exact_solution import (
ExactSolution,
)
from polaris.viz import plot_horiz_field
from polaris.viz import plot_horiz_field, use_mplstyle


class Viz(Step):
Expand Down Expand Up @@ -67,6 +67,7 @@ def run(self):
section = config['manufactured_solution']
eta0 = section.getfloat('ssh_amplitude')

use_mplstyle()
fig, axes = plt.subplots(nrows=nres, ncols=3, figsize=(12, 2 * nres))
rmse = []
error_range = None
Expand Down
14 changes: 2 additions & 12 deletions polaris/ocean/tasks/single_column/viz.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import sys
from typing import TYPE_CHECKING # noqa: F401

if TYPE_CHECKING or sys.version_info >= (3, 9, 0):
import importlib.resources as imp_res # noqa: F401
else:
# python <= 3.8
import importlib_resources as imp_res # noqa: F401

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

from polaris import Step
from polaris.viz import use_mplstyle


class Viz(Step):
Expand Down Expand Up @@ -47,9 +39,7 @@ def run(self):
"""
Run this step of the test case
"""
style_filename = str(
imp_res.files('polaris.viz') / 'polaris.mplstyle')
plt.style.use(style_filename)
use_mplstyle()
ideal_age = self.ideal_age
ds = xr.load_dataset('output.nc')
t_index = ds.sizes['Time'] - 1
Expand Down
Loading

0 comments on commit 1037179

Please sign in to comment.