Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 068eada

Browse files
committed
added date index plot faq
svn path=/trunk/matplotlib/; revision=5437
1 parent 41f558b commit 068eada

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

doc/devel/outline.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ time series plots ? no author ?
2222
date plots John has author ?
2323
working with data John has author Darren
2424
custom ticking ? no author ?
25-
masked data Eric has author ?
25+
masked data Eric has author ?
2626
text ? no author ?
2727
patches ? no author ?
2828
legends ? no author ?

doc/faq/howto_faq.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,41 @@ How do I use matplotlib with zope?
9191
----------------------------------
9292

9393
TODO
94+
95+
96+
.. _date-index-plots:
97+
98+
How do I skip dates where there is no data?
99+
===========================================
100+
101+
When plotting time series, eg financial time series, one often wants
102+
to leave out days on which there is no data, eg weekends. By passing
103+
in dates on the x-xaxis, you get large horizontal gaps on periods when
104+
there is not data. The solution is to pass in some proxy x-data, eg
105+
evenly sampled indicies, and then use a custom formatter to format
106+
these as dates. The example below shows how to use an 'index formatter'
107+
to achieve the desired plot
108+
109+
import numpy as np
110+
import matplotlib.pyplot as plt
111+
import matplotlib.mlab as mlab
112+
import matplotlib.ticker as ticker
113+
114+
r = mlab.csv2rec('../data/aapl.csv')
115+
r.sort()
116+
r = r[-30:] # get the last 30 days
117+
118+
N = len(r)
119+
ind = np.arange(N) # the evenly spaced plot indices
120+
121+
def format_date(x, pos=None):
122+
thisind = np.clip(int(x+0.5), 0, N-1)
123+
return r.date[thisind].strftime('%Y-%m-%d')
124+
125+
fig = plt.figure()
126+
ax = fig.add_subplot(111)
127+
ax.plot(ind, r.adj_close, 'o-')
128+
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
129+
fig.autofmt_xdate()
130+
131+
plt.show()

doc/make.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def check_build():
1414
except OSError:
1515
pass
1616

17+
def sf():
18+
'push a copy to the sf site'
19+
os.system('cd build; rsync -avz html [email protected]:/home/groups/m/ma/matplotlib/htdocs/doc/ -essh')
20+
1721
def figs():
1822
os.system('cd users/figures/ && python make.py')
1923

@@ -56,6 +60,7 @@ def all():
5660
'html':html,
5761
'latex':latex,
5862
'clean':clean,
63+
'sf':sf,
5964
'all':all,
6065
}
6166

0 commit comments

Comments
 (0)