|
| 1 | +import datetime |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | +from matplotlib.colors import LinearSegmentedColormap |
| 6 | + |
| 7 | +from .widget_base import WidgetMPL |
| 8 | + |
| 9 | + |
| 10 | +class SeasonWidget(WidgetMPL): |
| 11 | + def __init__(self, lv, text_colour="black", hemisphere="south", **kwargs): |
| 12 | + super().__init__(lv=lv, **kwargs) |
| 13 | + self.text_colour = text_colour |
| 14 | + self.hemisphere = hemisphere |
| 15 | + self.arrow = None |
| 16 | + |
| 17 | + def _make_mpl(self): |
| 18 | + plt.rc("axes", linewidth=4) |
| 19 | + plt.rc("font", weight="bold") |
| 20 | + fig, ax = plt.subplots(subplot_kw={"projection": "polar"}, figsize=(5, 5)) |
| 21 | + fig.patch.set_facecolor((0, 0, 0, 0)) # make background transparent |
| 22 | + ax.set_facecolor("white") # adds a white ring around edge |
| 23 | + |
| 24 | + # Setting up grid |
| 25 | + ax.set_rticks([]) |
| 26 | + ax.grid(False) |
| 27 | + ax.set_theta_zero_location("N") |
| 28 | + ax.set_theta_direction(-1) |
| 29 | + |
| 30 | + # Label Angles |
| 31 | + if self.hemisphere == "south": |
| 32 | + MONTH = ["Sum", "Aut", "Win", "Spr"] |
| 33 | + cmap = LinearSegmentedColormap.from_list( |
| 34 | + "custom_gradient", ["orange", "black", "blue", "black", "orange"] |
| 35 | + ) |
| 36 | + else: |
| 37 | + MONTH = ["Win", "Spr", "Sum", "Aut"] |
| 38 | + cmap = LinearSegmentedColormap.from_list( |
| 39 | + "custom_gradient", ["blue", "black", "orange", "black", "blue"] |
| 40 | + ) |
| 41 | + |
| 42 | + ANGLES = np.linspace(np.pi / 4, 2 * np.pi + np.pi / 4, 4, endpoint=False) |
| 43 | + ax.tick_params(axis="x", which="major", pad=12, labelcolor=self.text_colour) |
| 44 | + ax.set_xticks(ANGLES) |
| 45 | + ax.set_xticklabels(MONTH, size=20) |
| 46 | + ax.spines["polar"].set_color(self.text_colour) |
| 47 | + |
| 48 | + # Colour background based on time of year: |
| 49 | + dec22_doy = datetime.date(2001, 12, 22).timetuple().tm_yday - 1 |
| 50 | + dec22 = np.pi * 2.0 * dec22_doy / 365 # summer solstice |
| 51 | + r = np.linspace(0, 10, 5) |
| 52 | + theta = np.linspace(dec22, dec22 + 2 * np.pi, 500) |
| 53 | + R, T = np.meshgrid(r, theta) |
| 54 | + ax.pcolormesh(T, R, T, cmap=cmap, shading="gouraud") |
| 55 | + |
| 56 | + return fig, ax |
| 57 | + |
| 58 | + def _update_mpl(self, fig, ax, date: datetime.datetime = None, show_year=True): |
| 59 | + if show_year and date is not None: |
| 60 | + title = str(date.year) |
| 61 | + else: |
| 62 | + title = "" |
| 63 | + fig.suptitle( |
| 64 | + title, fontsize=20, fontweight="bold", y=0.08, color=self.text_colour |
| 65 | + ) |
| 66 | + |
| 67 | + if date is None: |
| 68 | + return |
| 69 | + else: |
| 70 | + day_of_year = date.timetuple().tm_yday - 1 |
| 71 | + position = day_of_year / 365.0 * np.pi * 2.0 |
| 72 | + self.arrow = ax.arrow( |
| 73 | + position, |
| 74 | + 0, |
| 75 | + 0, |
| 76 | + 8.5, |
| 77 | + facecolor="#fff", |
| 78 | + width=0.1, |
| 79 | + head_length=2, |
| 80 | + edgecolor="black", |
| 81 | + ) # , zorder=11, width=1) |
| 82 | + |
| 83 | + def _reset_mpl(self, fig, ax, **kwargs): |
| 84 | + fig.suptitle("") |
| 85 | + if self.arrow is not None: |
| 86 | + self.arrow.remove() |
0 commit comments