Click here to download this notebook.

[2]:
from pprint import pprint

import yaml
from cartopy import crs as ccrs
from casys.elements import CyclePassFmt

from casys import (
    AxeParams,
    CasysPlot,
    CyclePassFormatter,
    DataParams,
    DateHandler,
    Field,
    GridParams,
    NadirData,
    PlotParams,
    PlotTemplate,
    Position,
    TextParams,
)

NadirData.enable_loginfo()

How to use plot templates

PlotTemplate is an object allowing to specify properties for each element of a plot [doc].

Create a simple plot template

[5]:
t = PlotTemplate(
    title=AxeParams(label={"size": "xx-large", "weight": "bold"}),
    stat_bar=AxeParams(
        label={"fontsize": "large"},
        position="top",
        enabled=True,
    ),
)

Create a plot with this template, and set other params

[6]:
pparams = PlotParams(grid=True)

tparams = TextParams(title={"weight": "light", "color": "red"})

plot = CasysPlot(
    data=ad,
    data_name="Sigma 0 by day",
    stat="mean",
    template=t,
    plot_params=pparams,
    text_params=tparams,
)
plot.set_ticks(fmt="CP")
plot.show()
[6]:
../_images/howto_model_plot_templates_9_0.png

Get plot template

[7]:
t = plot.template

Store plot template

[8]:
t.store(name="simple_t", path="templates.yaml", overwrite=True)

Load the template

[9]:
tt = PlotTemplate.load(name="simple_t", path="templates.yaml")

Create another plot with this template

[10]:
plot = CasysPlot(data=ad, data_name="SLA pass", stat="mean", template=tt)
plot.show()
[10]:
../_images/howto_model_plot_templates_17_0.png

Create a template with altimetric ticks

[11]:
template = PlotTemplate(
    legend=AxeParams(label={"size": "large"}),
    title=AxeParams(label={"size": "x-large", "weight": "bold"}),
    plot=PlotParams(grid=True),
    stat_bar=AxeParams(
        label={"fontsize": "large"},
        position="top",
        enabled=True,
    ),
    x1=AxeParams(
        label={"label": "cycle"},
        values=CyclePassFormatter(orf=ad.orf, fmt=CyclePassFmt.C),
        enabled=True,
    ),
)
[12]:
plot = CasysPlot(data=ad, data_name="Sigma 0 by day", stat="mean", template=template)
plot.show()
[12]:
../_images/howto_model_plot_templates_20_0.png
[13]:
t = plot.template
[14]:
plot = CasysPlot(data=ad, data_name="SLA pass", stat="mean", template=t)
plot.show()
[14]:
../_images/howto_model_plot_templates_22_0.png

Create a carto plot and reuse its template

[15]:
pparams = PlotParams(
    fill_ocean=False,
    projection=ccrs.PlateCarree(central_longitude=180.0),
    grid=True,
    color_limits=(10, 17),
    x_limits=(40, 180),
    y_limits=(-90, 90),
)
tparams = TextParams(
    legend={
        "size": "large",
    },
    title={"size": "x-large", "weight": "bold"},
)
color_bar_param = AxeParams(
    label={"label": "m"}, ticks={"labelsize": "medium"}, enabled=True
)

plot = CasysPlot(
    data=ad,
    data_name="Sigma 0",
    plot="map",
    plot_params=pparams,
    text_params=tparams,
)

plot.add_color_bar(position="right", params=color_bar_param)
plot.add_hist_bar(position="bottom")
plot.set_title("Custom title")

plot.show()
[15]:
../_images/howto_model_plot_templates_24_0.png
[16]:
t = plot.template
[17]:
t.store(name="slabox_t", path="templates.yaml")
[18]:
tt = PlotTemplate.load(name="slabox_t", path="templates.yaml")
plot = CasysPlot(data=ad, data_name="Wind speed", plot="map", template=tt)
plot.show()
[18]:
../_images/howto_model_plot_templates_27_0.png

Create graph template

[19]:
t = PlotTemplate(
    plot=PlotParams(
        fill_ocean=False,
        projection=ccrs.PlateCarree(central_longitude=180.0),
        grid=True,
        color_limits=(10, 17),
        y_limits=(-90, 90),
        fig_height=4.5,
    ),
    title=AxeParams(label={"label": "A Title", "fontsize": "x-large"}, enabled=True),
    x1=AxeParams(
        label={"label": "default_x1_label", "fontsize": "medium"}, enabled=True
    ),
    x2=AxeParams(
        label={"label": "default_x2_label", "fontsize": "medium"}, enabled=True
    ),
    y1=AxeParams(
        label={"label": "default_y1_label", "fontsize": "medium"}, enabled=True
    ),
    y2=AxeParams(
        label={"label": "default_y2_label", "fontsize": "medium"}, enabled=True
    ),
    stat_bar=AxeParams(
        label={"fontsize": "small", "weight": "light"},
        position="top",
        enabled=True,
    ),
    text=TextParams(
        legend={
            "size": "large",
        },
        title={"label": "Other title", "weight": "bold"},
    ),
    ticks_spaces=[(Position.TOP, 0.1), (Position.BOTTOM, 0.1)],
)
[20]:
plot = CasysPlot(data=ad, data_name="Sigma 0", template=t)
plot.show()
[20]:
../_images/howto_model_plot_templates_30_0.png

Get the template and enrich it with set_template

[21]:
t = plot.template

t2 = PlotTemplate.set_template(
    template=t,
    data_params=DataParams(
        x_limits=(
            DateHandler("2019/06/04T07:00:00"),
            DateHandler("2019/06/04T10:00:00"),
        )
    ),
)
plot = CasysPlot(data=ad, data_name="Sigma 0", template=t2)
plot.show()
[21]:
../_images/howto_model_plot_templates_32_0.png

Create map template

[22]:
t = PlotTemplate(
    plot=PlotParams(
        fill_ocean=False,
        projection=ccrs.PlateCarree(central_longitude=180.0),
        grid=False,
        color_limits=(10, 17),
        y_limits=(-90, 90),
        fig_height=4.5,
    ),
    color_bar=AxeParams(position="right", enabled=True),
    stat_bar=AxeParams(
        label={"fontsize": "small", "weight": "light"},
        position="top",
        enabled=True,
    ),
    text=TextParams(
        legend={
            "size": "large",
        },
        title={"label": "title", "size": "x-large", "weight": "bold"},
    ),
    grid=GridParams(
        line_properties={"linestyle": "dashed"}, right_labels=False, enabled=True
    ),
    ticks_spaces=[(Position.TOP, 0.1), (Position.BOTTOM, 0.1)],
)
[23]:
plot = CasysPlot(data=ad, data_name="Wind speed", plot="map", template=t)

plot.show()
[23]:
../_images/howto_model_plot_templates_35_0.png

Get the template and enrich it with set_template

[24]:
t = plot.template

t2 = PlotTemplate.set_template(template=t, plot_params=PlotParams(color_map="autumn"))
[25]:
plot = CasysPlot(
    data=ad,
    data_name="Wind speed",
    plot="map",
    template=t2,
)

plot.show()
[25]:
../_images/howto_model_plot_templates_38_0.png

Merged plots

[26]:
tparams = TextParams(
    legend={"size": "large"},
    title={"size": "x-large", "weight": "bold"},
)

pparams = PlotParams(grid=True)

stat_bar_param = AxeParams(
    label={
        "fontsize": "large",
    }
)
[27]:
plot = CasysPlot(
    data=ad, data_name="SLA pass", stat="mean", plot_params=pparams, text_params=tparams
)
plot.set_legend("Sla")
plot.add_stat_bar(params=stat_bar_param)
plot.set_ticks(position="top", fmt="P")

plot2 = CasysPlot(data=ad, data_name="SIG0 pass", stat="mean")
plot2.set_legend("Sig0")
plot2.add_stat_bar(params=stat_bar_param)
plot.add_plot(plot2)

plot.set_text_size(elements=["axes_ticks"], size="large")

plot.set_title("Sentinel-6 SWH")
plot.set_ticks(fmt="CP")
plot.show()
[27]:
../_images/howto_model_plot_templates_41_0.png
[28]:
t = plot.template

t.store(name="merged_t", path="templates.yaml")
[29]:
plot = CasysPlot(data=ad, data_name="SLA pass", stat="mean", template=t)

t2 = plot2.template
plot2 = CasysPlot(data=ad, data_name="SIG0 pass", stat="mean", template=t2)

plot.add_plot(plot2)
plot.show()
[29]:
../_images/howto_model_plot_templates_43_0.png

Show yaml file content

[30]:
with open(r"templates.yaml") as file:
    templates = yaml.load(file, Loader=yaml.FullLoader)
    pprint(templates)
{'templates': {'merged_t': {'grid': {'properties': {'defaults': {'enabled': True}},
                                     'xlabel_style': {'fontsize': 'large'},
                                     'ylabel_style': {'fontsize': 'large'}},
                            'legend': {'label': {'defaults': {'fontsize': 'large'},
                                                 'users': {'label': 'Sla'}}},
                            'stat_bar': {'label': {'defaults': {'fontsize': 'large',
                                                                'label': 'Sla'},
                                                   'users': {'fontsize': 'large'}},
                                         'properties': {'enabled': True}},
                            'title': {'label': {'defaults': {'fontsize': 'x-large',
                                                             'weight': 'bold'},
                                                'users': {'label': 'Sentinel-6 '
                                                                   'SWH'}}},
                            'x1': {'properties': {'enabled': True},
                                   'ticks': {'values': {'fmt': 'CP',
                                                        'orf': {'name': 'C_J3_GDRD',
                                                                'type': 'VANILLA'}}}},
                            'x2': {'properties': {'enabled': True,
                                                  'is_copy': True},
                                   'ticks': {'labelsize': 'large',
                                             'values': {'fmt': 'P',
                                                        'orf': {'name': 'C_J3_GDRD',
                                                                'type': 'VANILLA'}}}},
                            'y1': {'ticks': {'labelsize': 'large'}},
                            'y2': {'ticks': {'labelsize': 'large'}}},
               'simple_t': {'grid': {'properties': {'defaults': {'enabled': True}}},
                            'stat_bar': {'label': {'defaults': {'fontsize': 'large'}},
                                         'properties': {'defaults': {'enabled': True,
                                                                     'position': 'TOP'}}},
                            'title': {'label': {'defaults': {'color': 'red',
                                                             'fontsize': 'xx-large',
                                                             'weight': 'light'}}},
                            'x1': {'properties': {'enabled': True},
                                   'ticks': {'values': {'fmt': 'CP',
                                                        'orf': {'name': 'C_J3_GDRD',
                                                                'type': 'VANILLA'}}}}},
               'slabox_t': {'color_bar': {'label': 'm',
                                          'properties': {'enabled': True,
                                                         'position': 'RIGHT'},
                                          'ticks': {'labelsize': 'medium'}},
                            'grid': {'properties': {'defaults': {'enabled': True}}},
                            'hist_bar_z': {'properties': {'defaults': {'axis': 'Z'},
                                                          'users': {'axis': 'Z',
                                                                    'enabled': True,
                                                                    'position': 'BOTTOM'}}},
                            'legend': {'label': {'defaults': {'fontsize': 'large'}}},
                            'plot': {'properties': {'defaults': {'color_limits': [10,
                                                                                  17],
                                                                 'fill_ocean': False,
                                                                 'projection': {'central_longitude': 180.0,
                                                                                'name': 'PlateCarree'},
                                                                 'x_limits': [40,
                                                                              180],
                                                                 'y_limits': [-90,
                                                                              90]}}},
                            'stat_bar': {'label': {'defaults': {'fontsize': 'large'}}},
                            'title': {'label': {'defaults': {'fontsize': 'x-large',
                                                             'weight': 'bold'},
                                                'users': {'label': 'Custom '
                                                                   'title'}}}}}}
[31]:
os.remove("templates.yaml")