Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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 requires a connection to Yahoo Finance, which is the data source.

  1. Determine the start date by subtracting one 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)
  2. We need to create the so-called locators. These objects from the matplotlib.dates package locate months and days on the x axis:

    alldays = DayLocator()
    months = MonthLocator()
  3. Create a date formatter to format the dates on the x axis. This formatter creates a string containing the short name of a month and the year:

    month_formatter = DateFormatter("%b %Y")
  4. Download the stock...