Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marking specific dates when visualizing a time series

I have a time series that has a few years' worth of data, for example this:

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))

ts = ts.cumsum()

ts.plot()

I also have two extra arrays: let's call the first

dates = [pd.datetime("2000-12-01"), pd.datetime("2001-01-03")]

And the second

labels = ["My birthday", "My dad's birthday"]

labels[i] contains the label for dates[i]. What I'd like to do is to display them in the time series graph so that they can be recognized. One possible visualization could be to display the date on the x axis, draw a vertical line starting from there and have the label either in a legend (with color coding) or somewhere next to the line.

The end result shouldn't be too different from this:

ExampleGraph

like image 608
Tex Avatar asked Mar 10 '16 02:03

Tex


People also ask

How do you plot time series data?

To create a time series plot in Excel, first select the time (DateTime in this case) Column and then the data series (streamflow in this case) column. Next, click on the Insert ribbon, and then select Scatter. From scatter plot options, select Scatter with Smooth Lines as shown below.

How do I change the date format in Plotly?

To customize date format, from 'Axes' under 'Style' menu choose 'Tick Labels' submenu. Next, select the axis you wish to modify, and then set 'advanced(d3-time-format)' for 'Label Format' attribute. A text box will appear where you can enter a custom time format.

What is the function used for plotting the values of a time series using Python?

In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.


1 Answers

Switching between pandas and matplotlib APIs can be confusing at first.

The solution: get the current axis and then use standard matplotlib API to annotate. This starts you off:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ts = pd.Series(np.random.randn(1000),
               index=pd.date_range('1/1/2000',
               periods=1000))

ts = ts.cumsum()
ts.plot()

label_list = [
    (pd.to_datetime("2001-05-01"), 'My\nbirthday', 'r'),
    (pd.to_datetime("2001-10-16"), "Dad's\nbirthday", 'b')
]

ax = plt.gca()

for date_point, label, clr in label_list:
    plt.axvline(x=date_point, color=clr)
    plt.text(date_point, ax.get_ylim()[1]-4, label,
             horizontalalignment='center',
             verticalalignment='center',
             color=clr,
             bbox=dict(facecolor='white', alpha=0.9))

plt.show()

This produces the image below, and you need to look into modifying titles, and text labels and their bounding boxes to the axis object:

example image

like image 191
daedalus Avatar answered Oct 25 '22 01:10

daedalus