Imagine that you want to shade the region of a stock chart, where the closing price is below average, with a different color than when it is above the mean. The fill_between
function is the best choice for the job. We will again omit the steps of downloading historical data going back 1
year, extracting dates and close prices, and creating locators and date formatter.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(dates, close)
plt.fill_between(dates, close.min(), close, where=close>close.mean(), facecolor="green", alpha=0.4) plt.fill_between(dates, close.min(), close, where=close<close.mean(), facecolor="red", alpha=0.4)
Now we can finish the plot by setting locators and formatting the x-axis values as dates. The stock price using conditional shading for DISH:
We shaded the region of a stock chart, where the closing price is below average, with a different color than when it is above the mean (see fillbetween.py
):
from matplotlib.finance import quotes_historical_yahoo from matplotlib.dates import DateFormatter from matplotlib.dates import DayLocator from matplotlib.dates import MonthLocator import sys from datetime import date import matplotlib.pyplot as plt import numpy as np today = date.today() start = (today.year - 1, today.month, today.day) symbol = 'DISH’ if len(sys.argv) == 2: symbol = sys.argv[1] quotes = quotes_historical_yahoo(symbol, start, today) quotes = np.array(quotes) dates = quotes.T[0] close = quotes.T[4] alldays = DayLocator() months = MonthLocator() month_formatter = DateFormatter("%b %Y") fig = plt.figure() ax = fig.add_subplot(111) ax.plot(dates, close) plt.fill_between(dates, close.min(), close, where=close>close.mean(), facecolor="green", alpha=0.4) plt.fill_between(dates, close.min(), close, where=close<close.mean(), facecolor="red", alpha=0.4) ax.xaxis.set_major_locator(months) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(month_formatter) ax.grid(True) fig.autofmt_xdate() plt.show()