Time for action – plotting a year’s worth of stock quotes
We can plot a year’s worth of stock quotes data with the matplotlib.finance
package. This will require a connection to Yahoo Finance, which will be the data source.
Determine the start date by subtracting
1
year from today.from matplotlib.dates import DateFormatter from matplotlib.dates import DayLocator from matplotlib.dates import MonthLocator from matplotlib.finance import quotes_historical_yahoo from matplotlib.finance import candlestick import sys from datetime import date import matplotlib.pyplot as plt today = date.today() start = (today.year - 1, today.month, today.day)
We need to create so-called locators. These objects from the
matplotlib.dates
package are needed to locate months and days on the x-axis.alldays = DayLocator() months = MonthLocator()
Create a date formatter to format the dates on the x axis. This formatter will create a string containing the short name of a month and the year.
month_formatter = DateFormatter(...