Skip to content

Commit

Permalink
Merge pull request #104 from FloraSauerbronn/create-plotting
Browse files Browse the repository at this point in the history
Creating plotting.py with pandas-flavor
  • Loading branch information
ocefpaf authored Jun 6, 2024
2 parents 0a60423 + 40f6e40 commit 2540a31
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gliderpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
from ._version import __version__
except ImportError:
__version__ = "unknown"

from .fetchers import GliderDataFetcher
from .plotting import plot_transect

__all__ = [
"GliderDataFetcher",
"plot_transect",
]
57 changes: 57 additions & 0 deletions gliderpy/plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Some convenience functions to help visualize glider data."""

from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

try:
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
except ModuleNotFoundError:
warnings.warn(
"gliderpy requires matplotlib and cartopy for plotting.",
stacklevel=1,
)
raise


if TYPE_CHECKING:
import pandas as pd

from pandas_flavor import register_dataframe_method


@register_dataframe_method
def plot_transect(
df: pd.DataFrame,
var: str,
**kw: dict,
) -> tuple(plt.Figure, plt.Axes):
"""Make a scatter plot of depth vs time coloured by a user defined
variable.
:param var: variable to colour the scatter plot
:return: figure, axes
"""
cmap = kw.get("cmap", None)

fig, ax = plt.subplots(figsize=(17, 2))
cs = ax.scatter(
df.index,
df["pressure"],
s=15,
c=df[var],
marker="o",
edgecolor="none",
cmap=cmap,
)

ax.invert_yaxis()
xfmt = mdates.DateFormatter("%H:%Mh\n%d-%b")
ax.xaxis.set_major_formatter(xfmt)

cbar = fig.colorbar(cs, orientation="vertical", extend="both")
cbar.ax.set_ylabel(var)
ax.set_ylabel("pressure")
return fig, ax

0 comments on commit 2540a31

Please sign in to comment.