Skip to content

Plotting Module#

The Plotting module provides functions for generating visualizations of bank lines, erosion results, and other data in the D-FAST Bank Erosion software.

Overview#

The Plotting module contains functions for creating various types of plots and visualizations that help users understand the results of bank line detection and erosion calculations. These visualizations include maps of bank lines, erosion profiles, and time series of erosion volumes.

Components#

The Plotting module consists of the following components:

Plotting Functions#

dfastbe.plotting #

Copyright (C) 2020 Stichting Deltares.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation version 2.1.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org/licenses/.

contact: delft3d.support@deltares.nl Stichting Deltares P.O. Box 177 2600 MH Delft, The Netherlands

All indications and logos of, and references to, "Delft3D" and "Deltares" are registered trademarks of Stichting Deltares, and remain the property of Stichting Deltares. All rights reserved.

INFORMATION This file is part of D-FAST Bank Erosion: https://github.com/Deltares/D-FAST_Bank_Erosion

BasePlot #

Source code in src/dfastbe/plotting.py
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
class BasePlot:

    def save_fig(self, fig: Figure, filename: Union[str, Path]) -> None:
        """
        Save a single figure to file.

        Arguments
        ---------
        fig : matplotlib.figure.Figure
            Figure to be saved.
        filename : str
            Name of the file to be written.
        """
        print("saving figure {file}".format(file=filename))
        matplotlib.pyplot.show(block=False)
        fig.savefig(filename, dpi=300)

    def set_size(self, fig: Figure) -> None:
        """
        Set the size of a figure.

        Currently, the size is hardcoded, but functionality may be extended in the
        future.

        Arguments
        ---------
        fig : matplotlib.figure.Figure
            Figure to be saved.
        """
        # the size of an a3 is (16.5, 11.75)
        # the size of an a3 is (16.5, 11.75)
        fig.set_size_inches(11.75, 8.25)  # a4

    def set_bbox(
        self,
        ax: Axes,
        bbox: Tuple[float, float, float, float],
        scale: float = 1000,
    ) -> None:
        """
        Specify the bounding limits of an axes object.

        Arguments
        ---------
        ax : matplotlib.axes.Axes
            Axes object to be adjusted.
        bbox : Tuple[float, float, float, float]
            Tuple containing boundary limits (xmin, ymin, xmax, ymax); unit m.
        scale: float
            Indicates whether the axes are in m (1) or km (1000).
        """
        ax.set_xlim(xmin=bbox[0] / scale, xmax=bbox[2] / scale)
        ax.set_ylim(ymin=bbox[1] / scale, ymax=bbox[3] / scale)

    def stations_marker(
        self,
        xykm: np.ndarray,
        ax: Axes,
        float_format: int = 1,
        scale: float = 1000,
    ) -> None:
        """
        Add markers indicating the river chainage to a plot.

        Arguments
        ---------
        xykm : np.ndarray
            Array containing the x, y, and chainage; unit m for x and y, km for chainage.
        ax : matplotlib.axes.Axes
            Axes object in which to add the markers.
        float_format : int
            Number of decimals used for marks.
        scale: float
            Indicates whether the axes are in m (1) or km (1000).
        """
        step = 10 ** (-float_format)
        label_str = " {:." + str(float_format) + "f}"
        km_rescaled = xykm[:, 2] / step
        mask = np.isclose(np.round(km_rescaled), km_rescaled)
        ax.plot(
            xykm[mask, 0] / scale,
            xykm[mask, 1] / scale,
            linestyle="None",
            marker="+",
            color="k",
        )
        for i in np.nonzero(mask)[0]:
            ax.text(
                xykm[i, 0] / scale,
                xykm[i, 1] / scale,
                label_str.format(xykm[i, 2]),
                fontsize="x-small",
                clip_on=True,
            )

    def plot_mesh(
        self,
        ax: Axes,
        xe: np.ndarray,
        ye: np.ndarray,
        scale: float = 1000,
    ) -> None:
        """
        Add a mesh to a plot.

        Arguments
        ---------
        ax : matplotlib.axes.Axes
            Axes object in which to add the mesh.
        xe : np.ndarray
            M x 2 array of begin/end x-coordinates of mesh edges.
        ye : np.ndarray
            M x 2 array of begin/end y-coordinates of mesh edges.
        scale : float
            Indicates whether the axes are in m (1) or km (1000).
        """
        xe1 = xe[:, (0, 1, 1)] / scale
        xe1[:, 2] = np.nan
        xev = xe1.reshape((xe1.size,))

        ye1 = ye[:, (0, 1, 1)] / scale
        ye1[:, 2] = np.nan
        yev = ye1.reshape((ye1.size,))

        # to avoid OverflowError: In draw_path: Exceeded cell block limit
        # plot the data in chunks ...
        for i in range(0, len(xev), 3000):
            ax.plot(
                xev[i : i + 3000],
                yev[i : i + 3000],
                color=(0.5, 0.5, 0.5),
                linewidth=0.25,
            )

    def plot_mesh_patches(
        self,
        ax: Axes,
        simulation_data: BaseSimulationData,
        minval: Optional[float] = None,
        maxval: Optional[float] = None,
        scale: float = 1000,
    ) -> matplotlib.collections.PolyCollection:
        """
        Add a collection of patches to the plot one for every face of the mesh.

        Arguments
        ---------
        ax : matplotlib.axes.Axes
            Axes object in which to add the mesh.
        minval : Optional[float]
            Lower limit for the color scale.
        maxval : Optional[float]
            Upper limit for the color scale.
        scale : float
            Indicates whether the axes are in m (1) or km (1000).

        Returns
        -------
        p : matplotlib.collections.PolyCollection
            Patches object.
        """
        tfn_list = []
        tval_list = []
        for n in range(3, max(simulation_data.n_nodes) + 1):
            mask = simulation_data.n_nodes >= n
            fn_masked = simulation_data.face_node[mask, :]
            tfn_list.append(fn_masked[:, (0, n - 2, n - 1)])
            tval_list.append(simulation_data.water_depth_face[mask])
        tfn = np.concatenate(tfn_list, axis=0)
        tval = np.concatenate(tval_list, axis=0)
        # cmap = matplotlib.pyplot.get_cmap('Spectral')
        if minval is None:
            minval = np.min(tval)
        if maxval is None:
            maxval = np.max(tval)
        p = ax.tripcolor(
            simulation_data.x_node / scale,
            simulation_data.y_node / scale,
            tfn,
            facecolors=tval,
            cmap="Spectral",
            vmin=minval,
            vmax=maxval,
        )
        return p

    def get_colors(self, cmap_name: str, n: int) -> List[Tuple[float, float, float]]:
        """
        Obtain N colors from the specified colormap.

        Arguments
        ---------
        cmap_name : str
            Name of the color map.
        n : int
            Number of colors to be returned.

        Returns
        -------
        clrcyc : List[Tuple[float, float, float]]
            List of colour tuplets.
        """
        cmap = matplotlib.cm.get_cmap(cmap_name)
        clrs = [cmap(i / (n - 1)) for i in range(n)]
        return clrs

    def zoom_x_and_save(
        self,
        fig: Figure,
        ax: Axes,
        figbase: Path,
        plot_ext: str,
        xzoom: List[Tuple[float, float]],
    ) -> None:
        """
        Zoom in on subregions of the x-axis and save the figure.

        Arguments
        ---------
        fig : matplotlib.figure.Figure
            Figure to be processed.
        ax : matplotlib.axes.Axes
            Axes to be processed.
        fig_base : str
            Base name of the figure to be saved.
        plot_ext : str
            File extension of the figure to be saved.
        xzoom : List[list[float,float]]
            Values at which to split the x-axis.
        """
        xmin, xmax = ax.get_xlim()
        for ix, zoom in enumerate(xzoom):
            ax.set_xlim(xmin=zoom[0], xmax=zoom[1])
            figfile = figbase.with_name(f"{figbase.stem}.sub{str(ix + 1)}{plot_ext}")
            self.save_fig(fig, figfile)
        ax.set_xlim(xmin=xmin, xmax=xmax)

    def zoom_xy_and_save(
        self,
        fig: Figure,
        ax: Axes,
        fig_base: Path,
        plot_ext: str,
        xyzoom: List[Tuple[float, float, float, float]],
        scale: float = 1000,
    ) -> None:
        """
        Zoom in on subregions in x,y-space and save the figure.

        Arguments
        ---------
        fig : matplotlib.figure.Figure
            Figure to be processed.
        ax : matplotlib.axes.Axes
            Axes to be processed.
        fig_base : str
            Base name of the figure to be saved.
        plot_ext : str
            File extension of the figure to be saved.
        xyzoom : List[List[float, float, float, float]]
            List of xmin, xmax, ymin, ymax values to zoom into.
        scale: float
            Indicates whether the axes are in m (1) or km (1000).
        """
        xmin, xmax = ax.get_xlim()
        ymin, ymax = ax.get_ylim()

        dx_zoom = 0
        xy_ratio = (ymax - ymin) / (xmax - xmin)
        for zoom in xyzoom:
            xmin0 = zoom[0]
            xmax0 = zoom[1]
            ymin0 = zoom[2]
            ymax0 = zoom[3]
            dx = xmax0 - xmin0
            dy = ymax0 - ymin0
            if dy < xy_ratio * dx:
                # x range limiting
                dx_zoom = max(dx_zoom, dx)
            else:
                # y range limiting
                dx_zoom = max(dx_zoom, dy / xy_ratio)
        dy_zoom = dx_zoom * xy_ratio

        for ix, zoom in enumerate(xyzoom):
            x0 = (zoom[0] + zoom[1]) / 2
            y0 = (zoom[2] + zoom[3]) / 2
            ax.set_xlim(
                xmin=(x0 - dx_zoom / 2) / scale, xmax=(x0 + dx_zoom / 2) / scale
            )
            ax.set_ylim(
                ymin=(y0 - dy_zoom / 2) / scale, ymax=(y0 + dy_zoom / 2) / scale
            )
            figfile = fig_base.with_name(f"{fig_base.stem}.sub{str(ix + 1)}{plot_ext}")
            self.save_fig(fig, figfile)

        ax.set_xlim(xmin=xmin, xmax=xmax)
        ax.set_ylim(ymin=ymin, ymax=ymax)


    def set_axes_properties(
        self,
        ax: Axes,
        chainage_txt: str,
        ylabel_txt: str,
        grid: bool,
        title_txt: str,
        handles: Optional[List[Any]] = None,
        labels: Optional[List[str]] = None,
    ) -> None:
        """
        Set the properties of the axes.

        Args:
            ax (Axes): The axes object to set properties for.
            chainage_txt (str): Label for the horizontal chainage axes.
            ylabel_txt (str): Label for the vertical axes.
            title_txt (str): Title for the plot.
        """
        ax.set_xlabel(chainage_txt)
        ax.set_ylabel(ylabel_txt)
        ax.grid(grid)
        ax.set_title(title_txt)
        if handles and labels:
            ax.legend(handles, labels, loc="upper right")
        else:
            ax.legend(loc="upper right")

    def save_plot(
        self,
        fig: Figure,
        ax: Axes,
        figure_index: int,
        plot_name: str,
        zoom_coords: Optional[List[Tuple[float, float, float, float]]],
        plot_flags: Dict[str, Any],
        zoom_xy: bool,
    ) -> int:
        """Save the plot to a file."""
        figure_index += 1
        fig_base = Path(plot_flags['fig_dir']) / f"{figure_index}_{plot_name}"
        if plot_flags["save_plot_zoomed"] and zoom_xy:
            self.zoom_xy_and_save(
                fig, ax, fig_base, plot_flags["plot_ext"], zoom_coords
            )
        elif plot_flags["save_plot_zoomed"]:
            self.zoom_x_and_save(fig, ax, fig_base, plot_flags["plot_ext"], zoom_coords)
        fig_path = fig_base.with_suffix(plot_flags["plot_ext"])
        self.save_fig(fig, fig_path)
        return figure_index

get_colors(cmap_name: str, n: int) -> List[Tuple[float, float, float]] #

Obtain N colors from the specified colormap.

Arguments#

cmap_name : str Name of the color map. n : int Number of colors to be returned.

Returns#

clrcyc : List[Tuple[float, float, float]] List of colour tuplets.

Source code in src/dfastbe/plotting.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def get_colors(self, cmap_name: str, n: int) -> List[Tuple[float, float, float]]:
    """
    Obtain N colors from the specified colormap.

    Arguments
    ---------
    cmap_name : str
        Name of the color map.
    n : int
        Number of colors to be returned.

    Returns
    -------
    clrcyc : List[Tuple[float, float, float]]
        List of colour tuplets.
    """
    cmap = matplotlib.cm.get_cmap(cmap_name)
    clrs = [cmap(i / (n - 1)) for i in range(n)]
    return clrs

plot_mesh(ax: Axes, xe: np.ndarray, ye: np.ndarray, scale: float = 1000) -> None #

Add a mesh to a plot.

Arguments#

ax : matplotlib.axes.Axes Axes object in which to add the mesh. xe : np.ndarray M x 2 array of begin/end x-coordinates of mesh edges. ye : np.ndarray M x 2 array of begin/end y-coordinates of mesh edges. scale : float Indicates whether the axes are in m (1) or km (1000).

Source code in src/dfastbe/plotting.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def plot_mesh(
    self,
    ax: Axes,
    xe: np.ndarray,
    ye: np.ndarray,
    scale: float = 1000,
) -> None:
    """
    Add a mesh to a plot.

    Arguments
    ---------
    ax : matplotlib.axes.Axes
        Axes object in which to add the mesh.
    xe : np.ndarray
        M x 2 array of begin/end x-coordinates of mesh edges.
    ye : np.ndarray
        M x 2 array of begin/end y-coordinates of mesh edges.
    scale : float
        Indicates whether the axes are in m (1) or km (1000).
    """
    xe1 = xe[:, (0, 1, 1)] / scale
    xe1[:, 2] = np.nan
    xev = xe1.reshape((xe1.size,))

    ye1 = ye[:, (0, 1, 1)] / scale
    ye1[:, 2] = np.nan
    yev = ye1.reshape((ye1.size,))

    # to avoid OverflowError: In draw_path: Exceeded cell block limit
    # plot the data in chunks ...
    for i in range(0, len(xev), 3000):
        ax.plot(
            xev[i : i + 3000],
            yev[i : i + 3000],
            color=(0.5, 0.5, 0.5),
            linewidth=0.25,
        )

plot_mesh_patches(ax: Axes, simulation_data: BaseSimulationData, minval: Optional[float] = None, maxval: Optional[float] = None, scale: float = 1000) -> matplotlib.collections.PolyCollection #

Add a collection of patches to the plot one for every face of the mesh.

Arguments#

ax : matplotlib.axes.Axes Axes object in which to add the mesh. minval : Optional[float] Lower limit for the color scale. maxval : Optional[float] Upper limit for the color scale. scale : float Indicates whether the axes are in m (1) or km (1000).

Returns#

p : matplotlib.collections.PolyCollection Patches object.

Source code in src/dfastbe/plotting.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def plot_mesh_patches(
    self,
    ax: Axes,
    simulation_data: BaseSimulationData,
    minval: Optional[float] = None,
    maxval: Optional[float] = None,
    scale: float = 1000,
) -> matplotlib.collections.PolyCollection:
    """
    Add a collection of patches to the plot one for every face of the mesh.

    Arguments
    ---------
    ax : matplotlib.axes.Axes
        Axes object in which to add the mesh.
    minval : Optional[float]
        Lower limit for the color scale.
    maxval : Optional[float]
        Upper limit for the color scale.
    scale : float
        Indicates whether the axes are in m (1) or km (1000).

    Returns
    -------
    p : matplotlib.collections.PolyCollection
        Patches object.
    """
    tfn_list = []
    tval_list = []
    for n in range(3, max(simulation_data.n_nodes) + 1):
        mask = simulation_data.n_nodes >= n
        fn_masked = simulation_data.face_node[mask, :]
        tfn_list.append(fn_masked[:, (0, n - 2, n - 1)])
        tval_list.append(simulation_data.water_depth_face[mask])
    tfn = np.concatenate(tfn_list, axis=0)
    tval = np.concatenate(tval_list, axis=0)
    # cmap = matplotlib.pyplot.get_cmap('Spectral')
    if minval is None:
        minval = np.min(tval)
    if maxval is None:
        maxval = np.max(tval)
    p = ax.tripcolor(
        simulation_data.x_node / scale,
        simulation_data.y_node / scale,
        tfn,
        facecolors=tval,
        cmap="Spectral",
        vmin=minval,
        vmax=maxval,
    )
    return p

save_fig(fig: Figure, filename: Union[str, Path]) -> None #

Save a single figure to file.

Arguments#

fig : matplotlib.figure.Figure Figure to be saved. filename : str Name of the file to be written.

Source code in src/dfastbe/plotting.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def save_fig(self, fig: Figure, filename: Union[str, Path]) -> None:
    """
    Save a single figure to file.

    Arguments
    ---------
    fig : matplotlib.figure.Figure
        Figure to be saved.
    filename : str
        Name of the file to be written.
    """
    print("saving figure {file}".format(file=filename))
    matplotlib.pyplot.show(block=False)
    fig.savefig(filename, dpi=300)

save_plot(fig: Figure, ax: Axes, figure_index: int, plot_name: str, zoom_coords: Optional[List[Tuple[float, float, float, float]]], plot_flags: Dict[str, Any], zoom_xy: bool) -> int #

Save the plot to a file.

Source code in src/dfastbe/plotting.py
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
def save_plot(
    self,
    fig: Figure,
    ax: Axes,
    figure_index: int,
    plot_name: str,
    zoom_coords: Optional[List[Tuple[float, float, float, float]]],
    plot_flags: Dict[str, Any],
    zoom_xy: bool,
) -> int:
    """Save the plot to a file."""
    figure_index += 1
    fig_base = Path(plot_flags['fig_dir']) / f"{figure_index}_{plot_name}"
    if plot_flags["save_plot_zoomed"] and zoom_xy:
        self.zoom_xy_and_save(
            fig, ax, fig_base, plot_flags["plot_ext"], zoom_coords
        )
    elif plot_flags["save_plot_zoomed"]:
        self.zoom_x_and_save(fig, ax, fig_base, plot_flags["plot_ext"], zoom_coords)
    fig_path = fig_base.with_suffix(plot_flags["plot_ext"])
    self.save_fig(fig, fig_path)
    return figure_index

set_axes_properties(ax: Axes, chainage_txt: str, ylabel_txt: str, grid: bool, title_txt: str, handles: Optional[List[Any]] = None, labels: Optional[List[str]] = None) -> None #

Set the properties of the axes.

Parameters:

Name Type Description Default
ax Axes

The axes object to set properties for.

required
chainage_txt str

Label for the horizontal chainage axes.

required
ylabel_txt str

Label for the vertical axes.

required
title_txt str

Title for the plot.

required
Source code in src/dfastbe/plotting.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def set_axes_properties(
    self,
    ax: Axes,
    chainage_txt: str,
    ylabel_txt: str,
    grid: bool,
    title_txt: str,
    handles: Optional[List[Any]] = None,
    labels: Optional[List[str]] = None,
) -> None:
    """
    Set the properties of the axes.

    Args:
        ax (Axes): The axes object to set properties for.
        chainage_txt (str): Label for the horizontal chainage axes.
        ylabel_txt (str): Label for the vertical axes.
        title_txt (str): Title for the plot.
    """
    ax.set_xlabel(chainage_txt)
    ax.set_ylabel(ylabel_txt)
    ax.grid(grid)
    ax.set_title(title_txt)
    if handles and labels:
        ax.legend(handles, labels, loc="upper right")
    else:
        ax.legend(loc="upper right")

set_bbox(ax: Axes, bbox: Tuple[float, float, float, float], scale: float = 1000) -> None #

Specify the bounding limits of an axes object.

Arguments#

ax : matplotlib.axes.Axes Axes object to be adjusted. bbox : Tuple[float, float, float, float] Tuple containing boundary limits (xmin, ymin, xmax, ymax); unit m. scale: float Indicates whether the axes are in m (1) or km (1000).

Source code in src/dfastbe/plotting.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def set_bbox(
    self,
    ax: Axes,
    bbox: Tuple[float, float, float, float],
    scale: float = 1000,
) -> None:
    """
    Specify the bounding limits of an axes object.

    Arguments
    ---------
    ax : matplotlib.axes.Axes
        Axes object to be adjusted.
    bbox : Tuple[float, float, float, float]
        Tuple containing boundary limits (xmin, ymin, xmax, ymax); unit m.
    scale: float
        Indicates whether the axes are in m (1) or km (1000).
    """
    ax.set_xlim(xmin=bbox[0] / scale, xmax=bbox[2] / scale)
    ax.set_ylim(ymin=bbox[1] / scale, ymax=bbox[3] / scale)

set_size(fig: Figure) -> None #

Set the size of a figure.

Currently, the size is hardcoded, but functionality may be extended in the future.

Arguments#

fig : matplotlib.figure.Figure Figure to be saved.

Source code in src/dfastbe/plotting.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def set_size(self, fig: Figure) -> None:
    """
    Set the size of a figure.

    Currently, the size is hardcoded, but functionality may be extended in the
    future.

    Arguments
    ---------
    fig : matplotlib.figure.Figure
        Figure to be saved.
    """
    # the size of an a3 is (16.5, 11.75)
    # the size of an a3 is (16.5, 11.75)
    fig.set_size_inches(11.75, 8.25)  # a4

stations_marker(xykm: np.ndarray, ax: Axes, float_format: int = 1, scale: float = 1000) -> None #

Add markers indicating the river chainage to a plot.

Arguments#

xykm : np.ndarray Array containing the x, y, and chainage; unit m for x and y, km for chainage. ax : matplotlib.axes.Axes Axes object in which to add the markers. float_format : int Number of decimals used for marks. scale: float Indicates whether the axes are in m (1) or km (1000).

Source code in src/dfastbe/plotting.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def stations_marker(
    self,
    xykm: np.ndarray,
    ax: Axes,
    float_format: int = 1,
    scale: float = 1000,
) -> None:
    """
    Add markers indicating the river chainage to a plot.

    Arguments
    ---------
    xykm : np.ndarray
        Array containing the x, y, and chainage; unit m for x and y, km for chainage.
    ax : matplotlib.axes.Axes
        Axes object in which to add the markers.
    float_format : int
        Number of decimals used for marks.
    scale: float
        Indicates whether the axes are in m (1) or km (1000).
    """
    step = 10 ** (-float_format)
    label_str = " {:." + str(float_format) + "f}"
    km_rescaled = xykm[:, 2] / step
    mask = np.isclose(np.round(km_rescaled), km_rescaled)
    ax.plot(
        xykm[mask, 0] / scale,
        xykm[mask, 1] / scale,
        linestyle="None",
        marker="+",
        color="k",
    )
    for i in np.nonzero(mask)[0]:
        ax.text(
            xykm[i, 0] / scale,
            xykm[i, 1] / scale,
            label_str.format(xykm[i, 2]),
            fontsize="x-small",
            clip_on=True,
        )

zoom_x_and_save(fig: Figure, ax: Axes, figbase: Path, plot_ext: str, xzoom: List[Tuple[float, float]]) -> None #

Zoom in on subregions of the x-axis and save the figure.

Arguments#

fig : matplotlib.figure.Figure Figure to be processed. ax : matplotlib.axes.Axes Axes to be processed. fig_base : str Base name of the figure to be saved. plot_ext : str File extension of the figure to be saved. xzoom : List[list[float,float]] Values at which to split the x-axis.

Source code in src/dfastbe/plotting.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def zoom_x_and_save(
    self,
    fig: Figure,
    ax: Axes,
    figbase: Path,
    plot_ext: str,
    xzoom: List[Tuple[float, float]],
) -> None:
    """
    Zoom in on subregions of the x-axis and save the figure.

    Arguments
    ---------
    fig : matplotlib.figure.Figure
        Figure to be processed.
    ax : matplotlib.axes.Axes
        Axes to be processed.
    fig_base : str
        Base name of the figure to be saved.
    plot_ext : str
        File extension of the figure to be saved.
    xzoom : List[list[float,float]]
        Values at which to split the x-axis.
    """
    xmin, xmax = ax.get_xlim()
    for ix, zoom in enumerate(xzoom):
        ax.set_xlim(xmin=zoom[0], xmax=zoom[1])
        figfile = figbase.with_name(f"{figbase.stem}.sub{str(ix + 1)}{plot_ext}")
        self.save_fig(fig, figfile)
    ax.set_xlim(xmin=xmin, xmax=xmax)

zoom_xy_and_save(fig: Figure, ax: Axes, fig_base: Path, plot_ext: str, xyzoom: List[Tuple[float, float, float, float]], scale: float = 1000) -> None #

Zoom in on subregions in x,y-space and save the figure.

Arguments#

fig : matplotlib.figure.Figure Figure to be processed. ax : matplotlib.axes.Axes Axes to be processed. fig_base : str Base name of the figure to be saved. plot_ext : str File extension of the figure to be saved. xyzoom : List[List[float, float, float, float]] List of xmin, xmax, ymin, ymax values to zoom into. scale: float Indicates whether the axes are in m (1) or km (1000).

Source code in src/dfastbe/plotting.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def zoom_xy_and_save(
    self,
    fig: Figure,
    ax: Axes,
    fig_base: Path,
    plot_ext: str,
    xyzoom: List[Tuple[float, float, float, float]],
    scale: float = 1000,
) -> None:
    """
    Zoom in on subregions in x,y-space and save the figure.

    Arguments
    ---------
    fig : matplotlib.figure.Figure
        Figure to be processed.
    ax : matplotlib.axes.Axes
        Axes to be processed.
    fig_base : str
        Base name of the figure to be saved.
    plot_ext : str
        File extension of the figure to be saved.
    xyzoom : List[List[float, float, float, float]]
        List of xmin, xmax, ymin, ymax values to zoom into.
    scale: float
        Indicates whether the axes are in m (1) or km (1000).
    """
    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()

    dx_zoom = 0
    xy_ratio = (ymax - ymin) / (xmax - xmin)
    for zoom in xyzoom:
        xmin0 = zoom[0]
        xmax0 = zoom[1]
        ymin0 = zoom[2]
        ymax0 = zoom[3]
        dx = xmax0 - xmin0
        dy = ymax0 - ymin0
        if dy < xy_ratio * dx:
            # x range limiting
            dx_zoom = max(dx_zoom, dx)
        else:
            # y range limiting
            dx_zoom = max(dx_zoom, dy / xy_ratio)
    dy_zoom = dx_zoom * xy_ratio

    for ix, zoom in enumerate(xyzoom):
        x0 = (zoom[0] + zoom[1]) / 2
        y0 = (zoom[2] + zoom[3]) / 2
        ax.set_xlim(
            xmin=(x0 - dx_zoom / 2) / scale, xmax=(x0 + dx_zoom / 2) / scale
        )
        ax.set_ylim(
            ymin=(y0 - dy_zoom / 2) / scale, ymax=(y0 + dy_zoom / 2) / scale
        )
        figfile = fig_base.with_name(f"{fig_base.stem}.sub{str(ix + 1)}{plot_ext}")
        self.save_fig(fig, figfile)

    ax.set_xlim(xmin=xmin, xmax=xmax)
    ax.set_ylim(ymin=ymin, ymax=ymax)

The plotting functions component provides functions for creating various types of visualizations, such as:

  • Maps of bank lines and erosion results
  • Profiles of bank erosion
  • Time series of erosion volumes
  • Visualizations of hydrodynamic data

Workflow#

The typical workflow for using the Plotting module is:

  1. Perform bank line detection or erosion calculation
  2. Call the appropriate plotting functions to visualize the results
  3. Display the plots or save them to files

Usage Example#

import matplotlib.pyplot as plt
from dfastbe import plotting as df_plt
from dfastbe.io.config import ConfigFile
from dfastbe.bank_erosion.bank_erosion import Erosion

# Load configuration file
config_file = ConfigFile.read("config.cfg")

# Initialize Erosion object
erosion = Erosion(config_file)

# Run erosion calculation
erosion.run()

# Create a plot of the results
fig, ax = plt.subplots(figsize=(10, 8))
df_plt.plot_bank_lines(ax, bank_lines, color='blue', linewidth=1.5)
df_plt.plot_erosion_results(ax, erosion_results, cmap='viridis')
plt.savefig("erosion_results.png")
plt.show()

For more details on the specific functions, refer to the API reference below.