Skip to content

Commit

Permalink
Merge pull request #236 from lubyant/New-feature--sector-offset
Browse files Browse the repository at this point in the history
Add feature: sector offset
  • Loading branch information
ocefpaf authored Jul 9, 2024
2 parents b4d4646 + 1f45313 commit fcd9789
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions windrose/windrose.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ def _init_plot(self, direction, var, **kwargs):
if nsector is None:
nsector = 16

sector_offset = kwargs.get("sectoroffset", 0)

# Sets the colors table based on the colormap or the "colors" argument
colors = kwargs.pop("colors", None)
cmap = kwargs.pop("cmap", None)
Expand All @@ -410,9 +412,10 @@ def _init_plot(self, direction, var, **kwargs):
var,
bins,
nsector,
total,
sector_offset,
normed,
blowto,
total,
)

return bins, nbins, nsector, colors, angles, kwargs
Expand Down Expand Up @@ -588,6 +591,12 @@ def bar(self, direction, var, **kwargs):
number of sectors used to compute the windrose table. If not set,
nsector=16, then each sector will be 360/16=22.5°, and the
resulting computed table will be aligned with the cardinals points.
sectoroffset: float, optional
the offset for the sectors between [-180/nsector, 180/nsector].
By default, the offset is zero, and the first sector is
[-360/nsector/2, 360/nsector/2] or [-11.25, 11.25] for nsector=16.
If offset is non-zero, the first sector will be
[-360/nsector + offset, 360/nsector + offset] and etc.
bins : 1D array or integer, optional
number of bins, or a sequence of bins variable. If not set, bins=6
between min(`var`) and max(`var`).
Expand Down Expand Up @@ -632,6 +641,9 @@ def bar(self, direction, var, **kwargs):

self._calm_circle()

# sector offset in radius
sector_offset = kwargs.pop("sectoroffset", 0) / 180 * np.pi

for j in range(nsector):
origin = 0
for i in range(nbins):
Expand All @@ -640,7 +652,7 @@ def bar(self, direction, var, **kwargs):
val = self._info["table"][i, j]
zorder = ZBASE + nbins - i
patch = mpl.patches.Rectangle(
(angles[j] - opening / 2, origin),
(angles[j] - opening / 2 - sector_offset, origin),
opening,
val,
facecolor=colors[i],
Expand Down Expand Up @@ -673,6 +685,11 @@ def box(self, direction, var, **kwargs):
number of sectors used to compute the windrose table. If not set,
nsector=16, then each sector will be 360/16=22.5°, and the
resulting computed table will be aligned with the cardinals points.
sectoroffset: float, optional
the offset for the sectors. By default, the offsect is zero, and
the first sector is [-360/nsector, 360/nsector] or [-11.25, 11.25]
for nsector=16. If offset is non-zero, the first sector will be
[-360/nsector + offset, 360/nsector + offset] and etc.
bins : 1D array or integer, optional
number of bins, or a sequence of bins variable. If not set, bins=6
between min(`var`) and max(`var`).
Expand Down Expand Up @@ -710,6 +727,9 @@ def box(self, direction, var, **kwargs):

self._calm_circle()

# sector offset in radius
sector_offset = kwargs.pop("sectoroffset", 0) / 180 * np.pi

for j in range(nsector):
origin = 0
for i in range(nbins):
Expand All @@ -718,7 +738,7 @@ def box(self, direction, var, **kwargs):
val = self._info["table"][i, j]
zorder = ZBASE + nbins - i
patch = mpl.patches.Rectangle(
(angles[j] - opening[i] / 2, origin),
(angles[j] - opening[i] / 2 - sector_offset, origin),
opening[i],
val,
facecolor=colors[i],
Expand Down Expand Up @@ -781,7 +801,16 @@ def pdf(
return (self, params)


def histogram(direction, var, bins, nsector, normed=False, blowto=False, total=0):
def histogram(
direction,
var,
bins,
nsector,
total,
sectoroffset=0,
normed=False,
blowto=False,
):
"""
Returns an array where, for each sector of wind
(centred on the north), we have the number of time the wind comes with a
Expand Down Expand Up @@ -812,11 +841,15 @@ def histogram(direction, var, bins, nsector, normed=False, blowto=False, total=0

angle = 360.0 / nsector

dir_bins = np.arange(-angle / 2, 360.0 + angle, angle, dtype=float)
dir_bins = np.arange(
-angle / 2 + sectoroffset,
360.0 + angle + sectoroffset,
angle,
dtype=float,
)
dir_edges = dir_bins.tolist()
dir_edges.pop(-1)
dir_edges[0] = dir_edges.pop(-1)
dir_bins[0] = 0.0

var_bins = bins.tolist()
var_bins.append(np.inf)
Expand Down

0 comments on commit fcd9789

Please sign in to comment.