开发者

matplotlib 'auto dimensioning of axes' not showing all datapoints

I recently moved from matplotlib version '0.99.1.1' to '1.0.1'. A new glitch i'm encountering has to do with "automatic axis dimensioning' ... not all datapoints end up being shown. Here is some code that reproduces my problem, some more discussion is below.

import datetime
from pylab import *
print matplotlib.__version__
x = [datetime.date(2011,2,11),
     datetime.date(2011,3,11),
     datetime.date(2011,4,11),
     datetime.date(2011,5,11),
     datetime.date(2011,6,11),
     datetime.date(2011,7,11)]
y = [23,41,67,72,18,19]
fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(x, y, 'kx')

# next task is to broaden the xaxis so that it begins 
# and ends at the start of a month (approximately).  
xmin, xmax, ymin, ymax = ax.axis() ; print xmin, xmax, ymin, ymax
a1 = xmin - min(x).day + 1
a2 = xmax - max(x).day + 31

#a1 = datetime.date(1,1,1) + datetime.timedelta(a1)
#a2 = datetime.date(1,1,1) + datetime.timedelta(a2)

#ax.axis([a1,a2,ymin,ymax]) #

ax.plot_date(a1, ymin, 'ko')
ax.plot_date(a2, ymin, 'ko')

show()

under 0.99.1, the above code worked fine as a workaround for (seemingly) not being able to reset the xaxis via an ax.axis(v) statement. Now, under 1.0.1, the two 'ko' points end up being outside the axes regardless of whe开发者_开发知识库ther ax.plot_date is called using a1 and a2 in units of 'days' or 'datetime.date'.

it is possible that the two 'ko' points are not being plotted. but to observe that they are infact being plotted, uncomment the ax.axis(v) call (which works fine in 1.0.1) and then look for the two quarter-circles in the bottom corners of the axes area.

while it is true that a cleaner way to expand the xaxis is to use an ax.axis(v) statement, the above behavior makes me nervous about the 'auto axes dimensioning' ... although its more likely that i am incorrectly coding something or another.

EDIT: fwiw ... the following code allows broadening to the 1st of the months, precisely

xmin, xmax, ymin, ymax = ax.axis() #; print xmin, xmax, ymin, ymax
a1 = datetime.date.fromordinal(int(xmin)) #; print 'a1= ', a1
a2 = datetime.date.fromordinal(int(xmax)) #; print 'a2= ', a2

y1, m1 = a1.year, a1.month 
y2, m2 = a2.year, a2.month + 1

a1 = datetime.date(y1,m1,1) #; print 'a1= ', a1
a2 = datetime.date(y2,m2,1) #; print 'a2= ', a2

ax.axis([a1,a2,ymin,ymax])


What's happening is that the axis isn't set to "automatically dimension" after you call ax.axis(). Calling axis turns off autoscaling (it assumes that if you're manually getting the axis limits, you probably don't want them to change).

Just add an ax.axis('auto') or an ax.set_autoscale_on() after you plot everything.

import datetime
import matplotlib.pyplot as plt
x = [datetime.date(2011,2,11),
     datetime.date(2011,3,11),
     datetime.date(2011,4,11),
     datetime.date(2011,5,11),
     datetime.date(2011,6,11),
     datetime.date(2011,7,11)]
y = [23,41,67,72,18,19]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(x, y, 'kx')

# next task is to broaden the xaxis so that it begins 
# and ends at the start of a month (approximately).  
xmin, xmax, ymin, ymax = ax.axis() ; print xmin, xmax, ymin, ymax
a1 = xmin - min(x).day + 1
a2 = xmax - max(x).day + 31

ax.plot_date(a1, ymin, 'ko')
ax.plot_date(a2, ymin, 'ko')
ax.axis('auto')

plt.show()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜