Skip to content

Instantly share code, notes, and snippets.

@zarch
Created April 18, 2012 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zarch/2412755 to your computer and use it in GitHub Desktop.
Save zarch/2412755 to your computer and use it in GitHub Desktop.
Plot week data as subplots
# -*- coding: utf-8 -*-
"""
____________________________________________________________
| +--------------------------------------------------------+ |
| ! ^ ___ --- ! |
| ! / \ _--_ / \ /\ / \ ! |
| ! / \__/ \ / \_-___-- \ / \ ! |
| ! / \___/ --__/ --__- ! |
| +-------+-------+-------+-------+-------+-------+--------+ |
| 04/02 04/03 04/04 04/02 04/03 04/04 04/04 |
| Mon Tue Wed Thu Fri Sat Sun |
| +--------------------------------------------------------+ |
| ! ^ ___ --- ! |
| ! / \ _--_ / \ /\ / \ ! |
| ! / \__/ \ / \_-___-- \ / \ ! |
| ! / \___/ --__/ --__- ! |
| +-------+-------+-------+-------+-------+-------+--------+ |
| 04/02 04/03 04/04 04/02 04/03 04/04 04/04 |
| Mon Tue Wed Thu Fri Sat Sun |
____________________________________________________________
1) I would like to see the xticks label for the first plot too,
at the moment it is shown only for the second subplot.
2) I would like to move the xticks label of 0.5 of the
xticks width, at the moment I have:
+-------+-------+-------+-------+-------+-------+--------+
04/02 04/03 04/04 04/02 04/03 04/04 04/04
Mon Tue Wed Thu Fri Sat Sun
and I would like this:
+-------+-------+-------+-------+-------+-------+--------+
04/02 04/03 04/04 04/02 04/03 04/04 04/04
Mon Tue Wed Thu Fri Sat Sun
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
import datetime as dt
def get_dates(start_day, stop_day, year=2012, month=4):
dates = []
for day in range(start_day, stop_day):
for hour in range(0, 24):
dates.append(dt.datetime(year, month, day, hour))
return np.array(dates)
DATAW = (
(get_dates(2, 9), np.random.random(24*7)),
(get_dates(9, 16), np.random.random(24*7)),
)
fig = plt.figure()
sub = 211
for dates, vals in DATAW:
ax = fig.add_subplot(sub)
ax.plot(dates, vals, 'o-')
ax.xaxis.set_major_locator( mdates.DayLocator() )
ax.xaxis.set_minor_locator( mdates.HourLocator(np.arange(0,25,6)) )
ax.xaxis.set_major_formatter( mdates.DateFormatter(u'%m/%d\n%a') )
fig.autofmt_xdate(rotation=0, ha='center')
plt.setp(ax.get_xticklabels(), visible=True)
plt.grid()
sub += 1
fig.subplots_adjust(hspace=.5)
plt.savefig("CO.pdf")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment