Stock volume varies a lot, so let’s plot it on a logarithmic scale. First we need to download historical data from Yahoo Finance, extract the dates and volume, create locators and a date formatter, create the figure, and add to it a subplot. We already went through these steps in the previous Time for action tutorial, so we will skip them here.
plt.semilogy(dates, volume)
Now set the locators and format the x-axis as dates. Instructions for these steps can be found in the previous Time for action tutorial as well. The stock volume using a logarithmic scale for DISH would appear as follows:
We plotted stock volume using a logarithmic scale (see logy.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] volume = quotes.T[5] alldays = DayLocator() months = MonthLocator() month_formatter = DateFormatter("%b %Y") fig = plt.figure() ax = fig.add_subplot(111) plt.semilogy(dates, volume) ax.xaxis.set_major_locator(months) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(month_formatter) fig.autofmt_xdate() plt.show