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

Creating plotting.py with pandas-flavor #104

Merged
merged 7 commits into from
Jun 6, 2024
Merged
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
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


FloraSauerbronn marked this conversation as resolved.
Show resolved Hide resolved
@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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
erddapy
httpx
pandas
pandas-flavor
xarray