Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spectral subplots #424

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions mikeio/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def __init__(self, da: "DataArray") -> None:
def __call__(self, ax=None, figsize=None, **kwargs):
# ax = self._get_ax(ax, figsize)
if self.da.n_frequencies > 0 and self.da.n_directions > 0:
return self._plot_2dspectrum(figsize=figsize, **kwargs)
return self._plot_2dspectrum(ax=ax, figsize=figsize, **kwargs)
elif self.da.n_frequencies == 0:
return self._plot_dirspectrum(ax=ax, figsize=figsize, **kwargs)
elif self.da.n_directions == 0:
Expand Down Expand Up @@ -591,7 +591,7 @@ def _plot_1dspectrum(self, x_values, ax=None, figsize=None, **kwargs):
ax.plot(x_values, y_values, **kwargs)
return ax

def _plot_2dspectrum(self, **kwargs):
def _plot_2dspectrum(self,ax=None, **kwargs):
values = self._get_first_step_values()

if "figsize" not in kwargs or kwargs["figsize"] is None:
Expand All @@ -605,6 +605,7 @@ def _plot_2dspectrum(self, **kwargs):
values,
frequencies=self.da.geometry.frequencies,
directions=self.da.geometry.directions,
ax=ax,
**kwargs,
)

Expand Down
10 changes: 8 additions & 2 deletions mikeio/spectral_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def plot_2dspectrum(
levels=None,
figsize=(7, 7),
add_colorbar=True,
ax=None,
):
"""
Plot spectrum in polar coordinates
Expand Down Expand Up @@ -52,6 +53,8 @@ def plot_2dspectrum(
specify size of figure, default (7, 7)
add_colorbar: bool, optional
Add colorbar to plot, default True
ax: matplotlib.axes, optional
Adding to existing axis, instead of creating new fig

Returns
-------
Expand Down Expand Up @@ -82,8 +85,11 @@ def plot_2dspectrum(
freq = 1.0 / np.flip(freq)
spectrum = np.fliplr(spectrum)

fig = plt.figure(figsize=figsize)
ax = plt.subplot(111, polar=True)
if ax is None:
fig = plt.figure(figsize=figsize)
ax = plt.subplot(111, polar=True)
else:
fig = plt.gcf()
ax.set_theta_direction(-1)
ax.set_theta_zero_location("N")

Expand Down