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

Skip to content

Commit 083f053

Browse files
committed
fixed up date demos
svn path=/trunk/matplotlib/; revision=3240
1 parent e927e25 commit 083f053

4 files changed

Lines changed: 39 additions & 49 deletions

File tree

examples/date_demo1.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
yahoo finance to get the data for plotting
1515
"""
1616

17-
from pylab import *
17+
from pylab import figure, show
1818
from matplotlib.finance import quotes_historical_yahoo
1919
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
2020
import datetime
@@ -33,7 +33,8 @@
3333
dates = [q[0] for q in quotes]
3434
opens = [q[1] for q in quotes]
3535

36-
ax = subplot(111)
36+
fig = figure()
37+
ax = fig.add_subplot(111)
3738
ax.plot_date(dates, opens, '-')
3839

3940
# format the ticks
@@ -46,7 +47,8 @@
4647
def price(x): return '$%1.2f'%x
4748
ax.fmt_xdata = DateFormatter('%Y-%m-%d')
4849
ax.fmt_ydata = price
50+
ax.grid(True)
51+
52+
fig.savefig('test')
4953

50-
savefig('test')
51-
grid(True)
5254
show()

examples/date_demo2.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
controlling major and minor ticks
66
"""
77
import datetime
8-
from pylab import *
8+
from pylab import figure, show
99
from matplotlib.dates import MONDAY, SATURDAY
1010
from matplotlib.finance import quotes_historical_yahoo
1111
from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter
@@ -26,19 +26,19 @@
2626

2727
dates = [q[0] for q in quotes]
2828
opens = [q[1] for q in quotes]
29-
30-
ax = subplot(111)
31-
plot_date(dates, opens, '-')
29+
30+
fig = figure()
31+
ax = fig.add_subplot(111)
32+
ax.plot_date(dates, opens, '-')
3233
ax.xaxis.set_major_locator(months)
3334
ax.xaxis.set_major_formatter(monthsFmt)
3435
ax.xaxis.set_minor_locator(mondays)
3536
ax.autoscale_view()
3637
#ax.xaxis.grid(False, 'major')
3738
#ax.xaxis.grid(True, 'minor')
39+
ax.grid(True)
3840

39-
labels = ax.get_xticklabels()
40-
setp(labels, rotation=45)
41+
fig.autofmt_xdate()
4142

42-
grid(True)
43-
savefig('date_demo2')
43+
#fig.savefig('date_demo2')
4444
show()

examples/date_demo_convert.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
#!/usr/bin/env python
22

3-
import matplotlib
4-
#matplotlib.use('TkAgg')
5-
from matplotlib import pylab
6-
PL = pylab
7-
from matplotlib.dates import DayLocator, HourLocator, \
8-
drange, date2num, timezone
93
import datetime
4+
from pylab import figure, show, nx
5+
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
106

11-
matplotlib.rcParams['timezone'] = 'US/Pacific'
12-
tz = timezone('US/Pacific')
137

14-
15-
date1 = datetime.datetime( 2000, 3, 2, tzinfo=tz)
16-
date2 = datetime.datetime( 2000, 3, 6, tzinfo=tz)
8+
date1 = datetime.datetime( 2000, 3, 2)
9+
date2 = datetime.datetime( 2000, 3, 6)
1710
delta = datetime.timedelta(hours=6)
1811
dates = drange(date1, date2, delta)
1912

20-
y = PL.arange( len(dates)*1.0)
21-
ysq = y*y
22-
23-
# note new constructor takes days or sequence of days you want to
24-
# tick, not the hour as before. Default is to tick every day
25-
majorTick = DayLocator(tz=tz)
13+
y = nx.arange( len(dates)*1.0)
2614

27-
# the hour locator takes the hour or sequence of hours you want to
28-
# tick, not the base multiple
29-
minorTick = HourLocator(range(0,25,6), tz=tz)
30-
ax = PL.subplot(111)
31-
ax.plot_date(dates, ysq, tz=tz)
15+
fig = figure()
16+
ax = fig.add_subplot(111)
17+
ax.plot_date(dates, y*y)
3218

3319
# this is superfluous, since the autoscaler should get it right, but
3420
# use date2num and num2date to to convert between dates and floats if
3521
# you want; both date2num and num2date convert an instance or sequence
36-
PL.xlim( dates[0], dates[-1] )
37-
ax.xaxis.set_major_locator(majorTick)
38-
ax.xaxis.set_minor_locator(minorTick)
39-
labels = ax.get_xticklabels()
40-
PL.setp(labels,'rotation', 90)
41-
PL.show()
22+
ax.set_xlim( dates[0], dates[-1] )
23+
24+
# The hour locator takes the hour or sequence of hours you want to
25+
# tick, not the base multiple
26+
27+
ax.xaxis.set_major_locator( DayLocator() )
28+
ax.xaxis.set_minor_locator( HourLocator(nx.arange(0,25,6)) )
29+
ax.xaxis.set_major_formatter( DateFormatter('%Y-%m-%d') )
30+
31+
ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
32+
fig.autofmt_xdate()
33+
34+
show()

examples/units/date_converter.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import date_support # set up the date converters
22
import datetime
3+
from matplotlib.dates import drange
34
from pylab import figure, show, nx
45

56

67
xmin = datetime.date(2007,1,1)
78
xmax = datetime.date.today()
8-
9-
xdates = [xmin]
10-
while 1:
11-
thisdate = xdates[-1] + datetime.timedelta(days=1)
12-
xdates.append(thisdate)
13-
if thisdate>=xmax: break
9+
delta = datetime.timedelta(days=1)
10+
xdates = drange(xmin, xmax, delta)
1411

1512
fig = figure()
1613
fig.subplots_adjust(bottom=0.2)
1714
ax = fig.add_subplot(111)
1815
ax.plot(xdates, nx.mlab.rand(len(xdates)), 'o')
1916
ax.set_xlim(datetime.date(2007,2,1), datetime.date(2007,3,1))
2017

21-
for label in ax.get_xticklabels():
22-
label.set_rotation(30)
23-
label.set_ha('right')
18+
fig.autofmt_xdate()
2419
show()

0 commit comments

Comments
 (0)