From 475c989a7cb2e24bc3c7720c87811d5e2a812efe Mon Sep 17 00:00:00 2001 From: FloraSauerbronn Date: Fri, 14 Jun 2024 15:24:36 -0300 Subject: [PATCH] Adding new function plot_ctd in to plotting.py --- gliderpy/plotting.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/gliderpy/plotting.py b/gliderpy/plotting.py index 0bbfdc4..5710a9e 100644 --- a/gliderpy/plotting.py +++ b/gliderpy/plotting.py @@ -9,6 +9,7 @@ import cartopy.crs as ccrs import matplotlib.dates as mdates import matplotlib.pyplot as plt + except ModuleNotFoundError: warnings.warn( "gliderpy requires matplotlib and cartopy for plotting.", @@ -81,4 +82,40 @@ def plot_transect( ax.set_ylabel("pressure") return fig, ax +@register_dataframe_method +def plot_ctd( + df: pd.DataFrame, + var: str, + ax: plt.Axes = None, + color: str = None +) -> tuple: + """Make a CTD profile plot of pressure vs property + depending on what variable was chosen. + + :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)[0])) + if ax is None: + fig, ax1 = plt.subplots(figsize=(5, 6)) + ax1.plot(profile[var], -profile["pressure"], label=var, color=color) + ax1.set_ylabel('Pressure') + ax1.set_xlabel(var) + ax1.legend() + return fig, ax1 + else: + fig = ax.get_figure() + ax2 = ax.twiny() # Create a new twinned axis + ax2.plot(profile[var], -profile["pressure"], label=var, color=color) + ax2.set_xlabel(var) + + # Handle legends + lines, labels = ax.get_legend_handles_labels() + lines2, labels2 = ax2.get_legend_handles_labels() + ax.legend(lines + lines2, labels + labels2, loc="lower center") + + return fig, ax2 \ No newline at end of file