-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from FloraSauerbronn/create-plotting
Creating plotting.py with pandas-flavor
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |