Skip to content

Commit

Permalink
Merge pull request #282 from MetOffice/240_improve_contour_plot
Browse files Browse the repository at this point in the history
Improve contour plot
  • Loading branch information
Sylviabohnenstengel authored Nov 24, 2023
2 parents 5001e65 + 10218d7 commit f9df008
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/source/reference/operators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ This page details the operators contained within CSET. It is automatically
generated from the code and its docstrings. Operators should be used via
:doc:`/usage/operator-recipes`.

CSET.operators.aggregate
------------------------

.. automodule:: CSET.operators.aggregate
:members:

CSET.operators.collapse
-----------------------

Expand Down
5 changes: 3 additions & 2 deletions src/CSET/operators/collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def collapse(
cube: iris.cube.Cube
Cube to collapse and iterate over one dimension
coordinate: str | list[str]
Coordinate(s) to collapse over i.e. 'time', 'longitude', 'latitude',
'model_level_number'. A list of multiple coordinates can be given.
Coordinate(s) to collapse over e.g. 'time', 'longitude', 'latitude',
'model_level_number', 'realization'. A list of multiple coordinates can
be given.
method: str
Type of collapse i.e. method: 'MEAN', 'MAX', 'MIN', 'MEDIAN',
'PERCENTILE' getattr creates iris.analysis.MEAN, etc For PERCENTILE
Expand Down
38 changes: 33 additions & 5 deletions src/CSET/operators/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import iris.cube
import iris.exceptions
import iris.plot as iplt
import iris.quickplot as qplt
import matplotlib.pyplot as plt
from markdown_it import MarkdownIt

Expand All @@ -49,9 +48,11 @@ def _make_plot_html_page(plot_filename: str) -> None:
}
.plot-container {
width: min(95vw, 95vh);
height: 99vh;
}
.plot-container>img {
width: 100%;
height: 100%;
}
#description-container {
flex: 30ch;
Expand Down Expand Up @@ -145,12 +146,39 @@ def spatial_contour_plot(
TypeError
If cube isn't a Cube.
"""
title = get_recipe_metadata().get("title", "Untitled")
if not filename:
filename = slugify(get_recipe_metadata().get("title", "Untitled"))
filename = slugify(title)
filename = Path(filename).with_suffix(".svg")
cube = _check_single_cube(cube)
qplt.contourf(cube)
plt.savefig(filename)

# with mpl.rc_context({"figure.labelsize": 22}):

# Setup plot details, size, resolution, etc.
# Set label size.
plt.figure(num=1, figsize=(15, 15), facecolor="w", edgecolor="k")
# fig.tight_layout(pad=0)

# plt.rc('xtick',labelsize=22)
# plt.rc('ytick',labelsize=22)

# Filled contour plot of the field.
iplt.contourf(cube)

# Add coastlines.
plt.gca().coastlines(resolution="10m")

# Set plotting limits.
# plt.xlim(points_x)
# plt.ylim(points_y)

# Add title.
plt.title(title, fontsize=16)
cbar = plt.colorbar()
cbar.set_label(label=f"{cube.name()} ({cube.units})", size=20)

plt.savefig(filename, bbox_inches="tight")

logging.info("Saved contour plot to %s", filename)
_make_plot_html_page(filename)
return cube
Expand Down Expand Up @@ -214,7 +242,7 @@ def postage_stamp_contour_plot(
colorbar = plt.colorbar(plot, colorbar_axes, orientation="horizontal")
colorbar.set_label(f"{cube.name()} / {cube.units}")

plt.savefig(filename)
plt.savefig(filename, bbox_inches="tight")
logging.info("Saved contour postage stamp plot to %s", filename)
_make_plot_html_page(filename)
return cube
Expand Down

0 comments on commit f9df008

Please sign in to comment.