Skip to content

Commit

Permalink
Added a plotting function for struct. 2d vector fields
Browse files Browse the repository at this point in the history
  • Loading branch information
LSchueler committed Jul 12, 2019
1 parent b12b9ae commit f708e28
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
17 changes: 15 additions & 2 deletions gstools/field/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,22 @@ def plot(self, field="field", fig=None, ax=None): # pragma: no cover
Default: `None`
"""
# just import if needed; matplotlib is not required by setup
from gstools.field.plot import plot_field
from gstools.field.plot import plot_field, plot_vec_field

# check if we have a vector field, this check should be sufficient
# for all but very edgy edge cases, which ate not going to be plotted
# anyway
if self.field.shape[0] == self.model.dim:
if self.model.dim == 2:
r = plot_vec_field(self, field, fig, ax)
else:
raise RuntimeError(
"Streamflow plotting only supported for 2d case."
)
else:
r = plot_field(self, field, fig, ax)

return plot_field(self, field, fig, ax)
return r

@property
def mean(self):
Expand Down
49 changes: 48 additions & 1 deletion gstools/field/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.. autosummary::
plot_field
plot_vec_field
"""
# pylint: disable=C0103
from __future__ import print_function, division, absolute_import
Expand All @@ -19,7 +20,7 @@
from gstools.tools import pos2xyz
from gstools.covmodel.plot import _get_fig_ax

__all__ = ["plot_field"]
__all__ = ["plot_field", "plot_vec_field"]


# plotting routines #######################################################
Expand Down Expand Up @@ -207,3 +208,49 @@ def update(__):
fig.colorbar(cont, cax=cax, ax=ax)
fig.show()
return ax

def plot_vec_field(fld, field="field", fig=None, ax=None): # pragma: no cover
"""
Plot a spatial random vector field.
Parameters
----------
fld : :class:`Field`
The given field class instance.
field : :class:`str`, optional
Field that should be plotted. Default: "field"
fig : :class:`Figure` or :any:`None`, optional
Figure to plot the axes on. If `None`, a new one will be created.
Default: `None`
ax : :class:`Axes` or :any:`None`, optional
Axes to plot on. If `None`, a new one will be added to the figure.
Default: `None`
"""
if fld.mesh_type is not "structured":
raise RuntimeError(
"Only structured vector fields are supported"
+ " for plotting. Please create one on a structured grid."
)
plot_field = getattr(fld, field)
assert not (fld.pos is None or plot_field is None)

norm = np.sqrt(plot_field[0, :].T ** 2 + plot_field[1, :].T ** 2)

fig, ax = _get_fig_ax(fig, ax)
title = "Field 2D " + fld.mesh_type + ": " + str(plot_field.shape)
x, y, __ = pos2xyz(fld.pos)

sp = plt.streamplot(
x,
y,
plot_field[0, :].T,
plot_field[1, :].T,
color=norm,
linewidth=norm / 2,
)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_title(title)
fig.colorbar(sp.lines)
fig.show()
return ax

0 comments on commit f708e28

Please sign in to comment.