#!/usr/bin/env python

"""

Double time representation. Bottom x-axis shows time in seconds-from-midnight
(sfm) fashion, whereas the top x-axis uses HH:MM:SS representation.

Initially written by Gokhan Sever with helps from Jae-Joon Lee.

Written: 2009-09-27

"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
from matplotlib.dates import DateFormatter, SecondLocator

# Prepare some pseudo data and time for seconds-from-midnight representation
ydata1 = np.random.random(100) * 1000
ydata2 = np.ones(100) / 2.
time = np.arange(35950, 36050)

fig = plt.figure()
host = SubplotHost(fig, 111)
fig.add_subplot(host)

# This is the heart of the example. We have to scale the axes correctly.
aux_trans = mtransforms.Affine2D().scale(1, ydata1.max())
par = host.twin(aux_trans)

aux_trans2 = mtransforms.Affine2D().translate(-1., 0.).scale(86400, 1)
par2 = host.twin(aux_trans2)

par.axis["top"].set_visible(False)
par2.axis["right"].set_visible(False)

host.set_xlabel("Time [sfm]")
host.set_ylabel("Data Source 1")
par.set_ylabel("Data Source 2")
par.axis["right"].label.set_visible(True)

p1, = host.plot(time, ydata1)
p2, = par.plot(time, ydata2)

host.axis["left"].label.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())

host.axis["bottom"].label.set_size(16)
host.axis["left"].label.set_size(16)
par.axis["right"].label.set_size(16)

# Move the title little upwards so it won't overlap with the ticklabels
title = plt.title("Double time: SFM and HH:MM:SS", fontsize=18)
title.set_position((0.5, 1.05))

# Modify here according to your need.
locator = SecondLocator(interval=20)
par2.xaxis.set_major_locator(locator)
par2.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))

plt.show()
