def color_dark(series: pd.Series, ax: matplotlib.axes.Axes = None, start: int = 19, end: int = 7): """Color dark phase in plot.""" assert is_datetime_or_timedelta_dtype( series.index ), f"Series must have datetime index but has {type(series.index)}" if not ax: ax = plt.gca() # get boundaries for dark times dark_mask = (series.index.hour >= start) | (series.index.hour < end) run_values, run_starts, run_lengths = find_runs(dark_mask) # highlighting for idx, is_dark in enumerate(run_values): if is_dark: start = run_starts[idx] end = run_starts[idx] + run_lengths[idx] - 1 ax.axvspan(series.index[start], series.index[end], alpha=0.5, color="gray") return ax
def color_dark( series: pd.Series, ax: matplotlib.axes.Axes = None, start: int = 19, end: int = 7 ): """Color dark phase in plot. Args: series (pd.Series) - Time-series variable ax (:class: `~matplotlib.axes.Axes`): axis to plot on (eg, `plt.gca()`) start (int): start of dark period/night end (hour): end of dark period/day Returns: ax (:class:`~matplotlib.axes._subplots.AxesSubplot`): Axes of plot """ assert is_datetime_or_timedelta_dtype( series.index ), f"Series must have datetime index but has {type(series.index)}" pd.plotting.register_matplotlib_converters() # prevents type error with axvspan if not ax: ax = plt.gca() # get boundaries for dark times dark_mask = (series.index.hour >= start) | (series.index.hour < end) run_values, run_starts, run_lengths = find_runs(dark_mask) for idx, is_dark in enumerate(run_values): if is_dark: start = run_starts[idx] end = run_starts[idx] + run_lengths[idx] - 1 ax.axvspan(series.index[start], series.index[end], alpha=0.5, color="gray") fig = plt.gcf() fig.autofmt_xdate() return ax
def set_ax_lims(self, ax: mpl.axes.Axes, xlims: tuple = None, ylims: tuple = None, yshade: list = None): """Set matplotlib axis limits and apply vertical shade. Keyword arguments: ax -- matplotlib.axes.Axes object to apply changes xlims -- tuple for setting x-axis limits (xmin, xmax) ylims -- tuple for setting y-axis limits (ymin, ymax) yshade -- list of tuples to apply axvspan matplotlib method """ if xlims is not None: ax.set_xlim(xlims) if ylims is not None: ax.set_ylim(ylims) if yshade is not None: for window in yshade: ax.axvspan(window[0], window[1], color='grey', alpha=0.75)