# Generated by pandoc-plot 1.8.0
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(2019)

import matplotlib.pyplot as plt
import pandas as pd


def discrete_colors(it):
    cmap = plt.get_cmap("inferno")
    mi, ma = 0.11, 0.75

    it = list(it)
    num = len(it)

    step = (ma - mi) / (num - 1)
    yield from zip([cmap(mi + i * step) for i in range(num)], it)


aapl = pd.read_csv(
    "files/rolling-stats/AAPL.csv",
    usecols=["Date", "Adjusted_close"],
    index_col="Date",
    parse_dates=["Date"],
)["Adjusted_close"]

fig, ax = plt.subplots(1, 1, figsize=(8, 4))

aapl.plot(ax=ax, label="AAPL closing price", color="k")
windows = [10, 30, 60]
for color, window in discrete_colors(windows):
    aapl.rolling(window).mean().plot(
        ax=ax, color=color, label=f"Rolling {window}d", linestyle="--"
    )
ax.legend()
ax.set_xlim([aapl.index.min(), aapl.index.max()])
ax.set_xlabel("Date")
ax.set_ylabel("Closing price [$US]")
Click here to see how this plot was generated.