Skip to content

Commit 860fc9a

Browse files
petebachantocefpaf
andauthored
WIP: Add ax and figsize arg to plotting functions (#121)
* Add ax argument to plot_windrose * Add figsize argument to plot functions * pre-commmit fixes * fix failing test * add subplots test * add baseline for new test Co-authored-by: Filipe Fernandes <[email protected]>
1 parent fa0a66a commit 860fc9a

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

tests/output/df/test_subplots.png

87.7 KB
Loading

tests/test_windrose_pandas.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import matplotlib
5+
import matplotlib.pyplot as plt
56
import numpy as np
67
import pandas as pd
78
import pytest
@@ -68,3 +69,11 @@ def test_windrose_np_plot_and_pd_plot():
6869
kind = "scatter"
6970
ax = plot_windrose(wd, ws, kind=kind, alpha=0.2)
7071
return ax.figure
72+
73+
74+
@pytest.mark.mpl_image_compare(baseline_dir="output/df")
75+
def test_subplots():
76+
fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw={"projection": "windrose"})
77+
plot_windrose(df, ax=ax1)
78+
plot_windrose(df, ax=ax2)
79+
return fig

windrose/windrose.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,22 @@ def __init__(self, *args, **kwargs):
9090
self.clear()
9191

9292
@staticmethod
93-
def from_ax(ax=None, fig=None, rmax=None, rect=None, *args, **kwargs):
93+
def from_ax(
94+
ax=None,
95+
fig=None,
96+
rmax=None,
97+
figsize=FIGSIZE_DEFAULT,
98+
rect=None,
99+
*args,
100+
**kwargs,
101+
):
94102
"""
95103
Return a WindroseAxes object for the figure `fig`.
96104
"""
97105
if ax is None:
98106
if fig is None:
99107
fig = plt.figure(
100-
figsize=FIGSIZE_DEFAULT,
108+
figsize=figsize,
101109
dpi=DPI_DEFAULT,
102110
facecolor="w",
103111
edgecolor="w",
@@ -693,10 +701,10 @@ def __init__(self, *args, **kwargs):
693701
super().__init__(*args, **kwargs)
694702

695703
@staticmethod
696-
def from_ax(ax=None, fig=None, *args, **kwargs):
704+
def from_ax(ax=None, fig=None, figsize=FIGSIZE_DEFAULT, *args, **kwargs):
697705
if ax is None:
698706
if fig is None:
699-
fig = plt.figure(figsize=FIGSIZE_DEFAULT, dpi=DPI_DEFAULT)
707+
fig = plt.figure(figsize=figsize, dpi=DPI_DEFAULT)
700708
ax = WindAxes(fig, 1, 1, 1, *args, **kwargs)
701709
fig.add_axes(ax)
702710
return ax
@@ -788,36 +796,36 @@ def histogram(direction, var, bins, nsector, normed=False, blowto=False):
788796

789797

790798
@docstring.copy(WindroseAxes.contour)
791-
def wrcontour(direction, var, ax=None, rmax=None, **kwargs):
799+
def wrcontour(direction, var, ax=None, rmax=None, figsize=FIGSIZE_DEFAULT, **kwargs):
792800
"""
793801
Draw contour probability density function and return Weibull
794802
distribution parameters.
795803
"""
796-
ax = WindroseAxes.from_ax(ax, rmax=rmax)
804+
ax = WindroseAxes.from_ax(ax, rmax=rmax, figsize=figsize)
797805
ax.contour(direction, var, **kwargs)
798806
ax.set_legend()
799807
return ax
800808

801809

802810
@docstring.copy(WindroseAxes.contourf)
803-
def wrcontourf(direction, var, ax=None, rmax=None, **kwargs):
804-
ax = WindroseAxes.from_ax(ax, rmax=rmax)
811+
def wrcontourf(direction, var, ax=None, rmax=None, figsize=FIGSIZE_DEFAULT, **kwargs):
812+
ax = WindroseAxes.from_ax(ax, rmax=rmax, figsize=figsize)
805813
ax.contourf(direction, var, **kwargs)
806814
ax.set_legend()
807815
return ax
808816

809817

810818
@docstring.copy(WindroseAxes.box)
811-
def wrbox(direction, var, ax=None, rmax=None, **kwargs):
812-
ax = WindroseAxes.from_ax(ax, rmax=rmax)
819+
def wrbox(direction, var, ax=None, rmax=None, figsize=FIGSIZE_DEFAULT, **kwargs):
820+
ax = WindroseAxes.from_ax(ax, rmax=rmax, figsize=figsize)
813821
ax.box(direction, var, **kwargs)
814822
ax.set_legend()
815823
return ax
816824

817825

818826
@docstring.copy(WindroseAxes.bar)
819-
def wrbar(direction, var, ax=None, rmax=None, **kwargs):
820-
ax = WindroseAxes.from_ax(ax, rmax=rmax)
827+
def wrbar(direction, var, ax=None, rmax=None, figsize=FIGSIZE_DEFAULT, **kwargs):
828+
ax = WindroseAxes.from_ax(ax, rmax=rmax, figsize=figsize)
821829
ax.bar(direction, var, **kwargs)
822830
ax.set_legend()
823831
return ax
@@ -833,23 +841,26 @@ def wrpdf(
833841
Nbins=10,
834842
ax=None,
835843
rmax=None,
844+
figsize=FIGSIZE_DEFAULT,
836845
*args,
837846
**kwargs,
838847
):
839848
"""
840849
Draw probability density function and return Weitbull distribution
841850
parameters
842851
"""
843-
ax = WindAxes.from_ax(ax)
852+
ax = WindAxes.from_ax(ax, figsize=figsize)
844853
ax, params = ax.pdf(var, bins, Nx, bar_color, plot_color, Nbins, *args, **kwargs)
845854
return (ax, params)
846855

847856

848-
def wrscatter(direction, var, ax=None, rmax=None, *args, **kwargs):
857+
def wrscatter(
858+
direction, var, ax=None, rmax=None, figsize=FIGSIZE_DEFAULT, *args, **kwargs
859+
):
849860
"""
850861
Draw scatter plot
851862
"""
852-
ax = WindroseAxes.from_ax(ax, rmax=rmax)
863+
ax = WindroseAxes.from_ax(ax, rmax=rmax, figsize=figsize)
853864
direction = -np.array(direction) + np.radians(90)
854865
ax.scatter(direction, var, *args, **kwargs)
855866
return ax
@@ -914,6 +925,7 @@ def plot_windrose(
914925
direction_name=DIR_DEFAULT,
915926
by=None,
916927
rmax=None,
928+
ax=None,
917929
**kwargs,
918930
):
919931
"""Plot windrose from a pandas DataFrame or a numpy array."""
@@ -924,7 +936,9 @@ def plot_windrose(
924936
direction = df[direction_name].values
925937
else:
926938
direction = direction_or_df
927-
return plot_windrose_np(direction, var, kind=kind, by=by, rmax=rmax, **kwargs)
939+
return plot_windrose_np(
940+
direction, var, kind=kind, by=by, rmax=rmax, ax=ax, **kwargs
941+
)
928942

929943

930944
def plot_windrose_df(
@@ -934,16 +948,24 @@ def plot_windrose_df(
934948
direction_name=DIR_DEFAULT,
935949
by=None,
936950
rmax=None,
951+
ax=None,
937952
**kwargs,
938953
):
939954
"""Plot windrose from a pandas DataFrame."""
940955
var = df[var_name].values
941956
direction = df[direction_name].values
942-
return plot_windrose_np(direction, var, by=by, rmax=rmax, **kwargs)
957+
return plot_windrose_np(direction, var, by=by, rmax=rmax, ax=ax, **kwargs)
943958

944959

945960
def plot_windrose_np(
946-
direction, var, kind="contour", clean_flag=True, by=None, rmax=None, **kwargs
961+
direction,
962+
var,
963+
kind="contour",
964+
clean_flag=True,
965+
by=None,
966+
rmax=None,
967+
ax=None,
968+
**kwargs,
947969
):
948970
"""Plot windrose from a numpy array."""
949971
if kind in D_KIND_PLOT.keys():
@@ -957,7 +979,7 @@ def plot_windrose_np(
957979
if clean_flag:
958980
direction, var = clean(direction, var)
959981
if by is None:
960-
ax = f_plot(direction=direction, var=var, rmax=rmax, **kwargs)
982+
ax = f_plot(direction=direction, var=var, rmax=rmax, ax=ax, **kwargs)
961983
if kind not in ["pdf"]:
962984
ax.set_legend()
963985
return ax

0 commit comments

Comments
 (0)