Beispiel #1
0
 def plot_track(
     self,
     axis: Axis = None,
     show: bool = False,
     color: str = 'k',
     coastline: bool = True,
     **kwargs,
 ):
     kwargs.update({'color': color})
     if axis is None:
         fig = pyplot.figure()
         axis = fig.add_subplot(111)
     data = self.data
     for i, (_, row) in enumerate(data.iterrows()):
         # when dealing with nautical degrees, U is sine and V is cosine.
         U = row['speed'] * numpy.sin(numpy.deg2rad(row['direction']))
         V = row['speed'] * numpy.cos(numpy.deg2rad(row['direction']))
         axis.quiver(row['longitude'], row['latitude'], U, V, **kwargs)
         if i % 6 == 0:
             axis.annotate(
                 row['datetime'],
                 (row['longitude'], row['latitude']),
             )
     if show:
         axis.axis('scaled')
     if bool(coastline) is True:
         plot_coastline(axis, show)
Beispiel #2
0
def plot_heated_stacked_area(df: pd.DataFrame,
                             lines: str,
                             heat: str,
                             backtest: str = None,
                             reset_y_lim: bool = False,
                             figsize: Tuple[int, int] = (16, 9),
                             color_map: str = 'afmhot',
                             ax: Axis = None,
                             upper_lower_missing_scale: float = 0.05) -> Axis:
    color_function = plt.get_cmap(color_map)
    x = df.index
    y = df[lines].values
    c = df[heat].values
    b = df[backtest].values if backtest is not None else None

    # assert enough data
    assert len(y.shape) > 1 and len(
        c.shape) > 1, "lines and heat need to be 2 dimensions!"

    # make sure we have one more line as heats
    if c.shape[1] == y.shape[1] + 1:
        lower = np.full((c.shape[0], 1),
                        y.min() * (1 - upper_lower_missing_scale))
        upper = np.full((c.shape[0], 1),
                        y.max() * (1 + upper_lower_missing_scale))
        y = np.hstack([lower, y, upper])

    # check for matching columns
    assert y.shape[1] - 1 == c.shape[
        1], f'unexpeced shapes: {y.shape[1] - 1} != {c.shape[1]}'

    _, ax = plt.subplots(figsize=figsize) if ax is None else (None, ax)

    ax.plot(x, y, color='k', alpha=0.0)

    for ci in range(c.shape[1]):
        for xi in range(len(x)):
            ax.fill_between(x[xi - 1:xi + 1],
                            y[xi - 1:xi + 1, ci],
                            y[xi - 1:xi + 1, ci + 1],
                            facecolors=color_function(c[xi - 1:xi + 1, ci]))

        if ci > 0:
            # todo annotate all first last and only convert date if it is actually a date
            ax.annotate(f'{y[-1, ci]:.2f}',
                        xy=(mdates.date2num(x[-1]), y[-1, ci]),
                        xytext=(4, -4),
                        textcoords='offset pixels')

    # reset limits
    ax.autoscale(tight=True)
    if reset_y_lim:
        ax.set_ylim(bottom=y[:, 1].min(), top=y[:, -1].max())

    # backtest
    if backtest:
        ax.plot(x, b)

    return ax