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
58 changes: 58 additions & 0 deletions gliderpy/plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Some convenience functions to help visualize glider data."""

from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

try:
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.",
stacklevel=1,
)
raise


if TYPE_CHECKING:
import pandas as pd

from pandas_flavor import register_dataframe_method

@register_dataframe_method

FloraSauerbronn marked this conversation as resolved.
Show resolved Hide resolved
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["depth (m)"],
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
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("Depth (m)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ax.set_ylabel("Depth (m)")
ax.set_ylabel("pressure")

return fig, ax