|
From: <jd...@us...> - 2008-06-09 17:49:16
|
Revision: 5436
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5436&view=rev
Author: jdh2358
Date: 2008-06-09 10:47:32 -0700 (Mon, 09 Jun 2008)
Log Message:
-----------
added date index formatter example
Added Paths:
-----------
trunk/matplotlib/examples/api/date_index_formatter.py
Added: trunk/matplotlib/examples/api/date_index_formatter.py
===================================================================
--- trunk/matplotlib/examples/api/date_index_formatter.py (rev 0)
+++ trunk/matplotlib/examples/api/date_index_formatter.py 2008-06-09 17:47:32 UTC (rev 5436)
@@ -0,0 +1,36 @@
+"""
+When plotting time series, eg financial time series, one often wants
+to leave out days on which there is no data, eh weekends. The example
+below shows how to use an 'index formatter' to achieve the desired plot
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+import matplotlib.mlab as mlab
+import matplotlib.ticker as ticker
+
+r = mlab.csv2rec('../data/aapl.csv')
+r.sort()
+r = r[-30:] # get the last 30 days
+
+
+# first we'll do it the default way, with gaps on weekends
+fig = plt.figure()
+ax = fig.add_subplot(111)
+ax.plot(r.date, r.adj_close, 'o-')
+fig.autofmt_xdate()
+
+# next we'll write a custom formatter
+N = len(r)
+ind = np.arange(N) # the evenly spaced plot indices
+
+def format_date(x, pos=None):
+ thisind = np.clip(int(x+0.5), 0, N-1)
+ return r.date[thisind].strftime('%Y-%m-%d')
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+ax.plot(ind, r.adj_close, 'o-')
+ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
+fig.autofmt_xdate()
+
+plt.show()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|