Skip to Content
Python: Real World Machine Learning
book

Python: Real World Machine Learning

by Prateek Joshi, John Hearty, Bastiaan Sjardin, Luca Massaron, Alberto Boschetti
November 2016
Beginner to intermediate
941 pages
21h 55m
English
Packt Publishing
Content preview from Python: Real World Machine Learning

Plotting date-formatted time series data

Let's look at how to plot time series data using date formatting. This is useful in visualizing stock data over time.

How to do it…

  1. Create a new Python file, and import the following packages:
    import numpy
    import matplotlib.pyplot as plt
    from matplotlib.mlab import csv2rec
    import matplotlib.cbook as cbook
    from matplotlib.ticker import Formatter
  2. Define a function to format the dates. The __init__ function sets the class variables:
    # Define a class for formatting
    class DataFormatter(Formatter):
        def __init__(self, dates, date_format='%Y-%m-%d'):
            self.dates = dates
            self.date_format = date_format
  3. Extract the value at any given time and return it in the following format:
     # Extact the value at time t at position 'position' ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Interpretable Machine Learning with Python

Interpretable Machine Learning with Python

Serg Masís
Large Scale Machine Learning with Python

Large Scale Machine Learning with Python

Luca Massaron, Alberto Boschetti, Bastiaan Sjardin

Publisher Resources

ISBN: 9781787123212Supplemental ContentPurchase Link