Skip to content

Commit

Permalink
Merge pull request #115 from FloraSauerbronn/ctd_plots
Browse files Browse the repository at this point in the history
Adding function ctd_plot and solving conflicts
  • Loading branch information
ocefpaf committed Aug 6, 2024
2 parents 6ae981f + 223bd4c commit c9df903
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
33 changes: 33 additions & 0 deletions gliderpy/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,36 @@ def plot_transect(
ax.set_ylim(ax.get_ylim()[0], 0)

return fig, ax


@register_dataframe_method
def plot_ctd(
df: pd.DataFrame,
profile_number: int,
var: str,
ax: plt.Axes = None,
color: str | None = None,
) -> tuple:
"""Make a CTD profile plot of pressure vs property
depending on what variable was chosen.
:param profile_number: profile number of CTD
:param var: variable to plot against pressure
:param ax: existing axis to plot on (default: None)
:param color: color for the plot line (default: None)
:return: figure, axes
"""
g = df.groupby(["longitude", "latitude"])
profile = g.get_group(list(g.groups)[profile_number])

if ax is None:
fig, ax = plt.subplots(figsize=(5, 6))
else:
fig = ax.get_figure()

ax.plot(profile[var], profile["pressure"], label=var, color=color)
ax.set_ylabel("Pressure")
ax.set_xlabel(var)
ax.invert_yaxis()

return fig, ax
Binary file added tests/baseline/test_plot_ctd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest

from gliderpy.fetchers import GliderDataFetcher
from gliderpy.plotting import plot_track, plot_transect
from gliderpy.plotting import plot_ctd, plot_track, plot_transect

root = Path(__file__).parent

Expand All @@ -34,7 +34,7 @@ def test_plot_track(glider_data):
@pytest.mark.mpl_image_compare(baseline_dir=root.joinpath("baseline/"))
def test_plot_transect(glider_data):
"""Test plot_transect accessor."""
fig, ax = plot_transect(glider_data, "temperature", cmap="viridis")
fig, ax = plot_transect(glider_data, var="temperature", cmap="viridis")
return fig


Expand All @@ -50,23 +50,18 @@ def test_plot_transect_multiple_figures(glider_data):
glider_data.plot_transect(var="temperature", ax=ax0, cmap="viridis")
glider_data.plot_transect(var="salinity", ax=ax1, cmap="cividis")

# Check if the y-label is named "pressure"
assert ax0.get_ylabel() == "pressure"
assert ax1.get_ylabel() == "pressure"

# Since sharex=True and sharey=True, xlim and ylim should be the same
assert ax0.get_xlim() == ax1.get_xlim()
assert ax0.get_ylim() == ax1.get_ylim()

# Get the colorbars
cbar0 = ax0.collections[0].colorbar
cbar1 = ax1.collections[0].colorbar

# Check colormap
assert cbar0.cmap.name == "viridis"
assert cbar1.cmap.name == "cividis"

# Check labels
assert cbar0.ax.get_ylabel() == "temperature"
assert cbar1.ax.get_ylabel() == "salinity"

Expand All @@ -78,3 +73,10 @@ def test_plot_transect_size(glider_data):
fig, ax = plt.subplots(figsize=(15, 9))
glider_data.plot_transect(var="temperature")
np.testing.assert_array_equal(fig.get_size_inches(), np.array([15.0, 9.0]))


@pytest.mark.mpl_image_compare(baseline_dir=root.joinpath("baseline/"))
def test_plot_ctd(glider_data):
"""Test plot_ctd accessor."""
fig, ax = plot_ctd(glider_data, 0, var="temperature", color="blue")
return fig

0 comments on commit c9df903

Please sign in to comment.