diff --git a/CHANGELOG.md b/CHANGELOG.md index ee63b35..485c288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ in every released version. * Fix issue where sometimes the plot sectors showed a straight line instead of a curved one (#137) * `wrscatter` now converts the wind direction from degrees to radians, so scatter plots use the same North-up, clockwise convention as the other plots (#99) +* Radial tick labels show percentages when a wind rose uses `normed=True` (#294) ### Version 1.7.0 diff --git a/tests/output/df/test_bar.png b/tests/output/df/test_bar.png index 42c7621..ae98982 100644 Binary files a/tests/output/df/test_bar.png and b/tests/output/df/test_bar.png differ diff --git a/tests/output/func/test_wrbar.png b/tests/output/func/test_wrbar.png index 59b5fda..ae98982 100644 Binary files a/tests/output/func/test_wrbar.png and b/tests/output/func/test_wrbar.png differ diff --git a/tests/output/oo/test_windrose_stacked_histogram_normed.png b/tests/output/oo/test_windrose_stacked_histogram_normed.png index 42c7621..ae98982 100644 Binary files a/tests/output/oo/test_windrose_stacked_histogram_normed.png and b/tests/output/oo/test_windrose_stacked_histogram_normed.png differ diff --git a/tests/output/oo/test_windrose_stacked_histogram_normed_calm_limit.png b/tests/output/oo/test_windrose_stacked_histogram_normed_calm_limit.png index 8526f40..dd10771 100644 Binary files a/tests/output/oo/test_windrose_stacked_histogram_normed_calm_limit.png and b/tests/output/oo/test_windrose_stacked_histogram_normed_calm_limit.png differ diff --git a/tests/output/test_bar_from_factory.png b/tests/output/test_bar_from_factory.png index 42c7621..ae98982 100644 Binary files a/tests/output/test_bar_from_factory.png and b/tests/output/test_bar_from_factory.png differ diff --git a/tests/test_windrose.py b/tests/test_windrose.py index ec70e82..282d094 100644 --- a/tests/test_windrose.py +++ b/tests/test_windrose.py @@ -78,6 +78,54 @@ def test_theta_labels(): plt.close() +@pytest.mark.parametrize("kind", ["bar", "box", "contour", "contourf"]) +@pytest.mark.parametrize("normed, suffix", [(False, ""), (True, "%")]) +def test_radial_label_units(kind, normed, suffix): + data = pd.DataFrame({"direction": [0, 90], "speed": [1, 2]}) + ax = plot_windrose(data, kind=kind, normed=normed) + try: + expected = ( + ["10%", "20%", "30%", "40%", "50%"] + if normed + else [ + "0.2", + "0.4", + "0.6", + "0.8", + "1.0", + ] + ) + assert [tick.get_text() for tick in ax.get_yticklabels()] == expected + + ax.set_rmax(12.5) + ax.set_radii_angle(angle=90) + expected = [f"{r:.1f}{suffix}" for r in (2.5, 5, 7.5, 10, 12.5)] + assert [tick.get_text() for tick in ax.get_yticklabels()] == expected + + ax.clear() + ax.set_rmax(10) + ax.set_radii_angle() + assert [tick.get_text() for tick in ax.get_yticklabels()] == [ + "2", + "4", + "6", + "8", + "10", + ] + finally: + plt.close(ax.figure) + + +def test_tex_radial_labels_escape_percent_sign(): + ax = WindroseAxes.from_ax() + try: + ax.bar(np.array([0, 90]), np.array([1, 2]), normed=True) + ax.set_radii_angle(usetex=True) + assert ax.get_yticklabels()[-1].get_text() == r"50\%" + finally: + plt.close(ax.figure) + + def test_windrose_incorrect_bins_specified(): ax = WindroseAxes.from_ax() with pytest.raises(ValueError) as exc_info: diff --git a/windrose/windrose.py b/windrose/windrose.py index b90861e..8836f84 100644 --- a/windrose/windrose.py +++ b/windrose/windrose.py @@ -149,6 +149,7 @@ def clear(self): self.patches_list = [] self.calm_count = None + self._normed = False def _colors(self, cmap, n): """ @@ -174,7 +175,11 @@ def set_radii_angle(self, **kwargs): fmt = "%d" else: fmt = "%.1f" - radii_labels = [fmt % r for r in radii] + suffix = "" + if self._normed: + usetex = kwargs.get("usetex", mpl.rcParams["text.usetex"]) + suffix = r"\%" if usetex else "%" + radii_labels = [(fmt % r) + suffix for r in radii] # radii_labels[0] = "" # Removing label 0 self.set_rgrids( radii=radii[1:], @@ -310,6 +315,7 @@ def _init_plot(self, direction, var, **kwargs): Other Parameters ---------------- normed : boolean, default False + Express the sector frequencies and radial labels as percentages. blowto : boolean, default False colors : str or list of str, default None The colors of the plot. @@ -455,6 +461,7 @@ def _init_plot(self, direction, var, **kwargs): normed, blowto, ) + self._normed = normed return bins, nbins, nsector, colors, angles, kwargs