matplotlib.pyplot.annotate() : place a text on subplot with 'axes fraction' coordinates

1.9k views Asked by At

I have some trouble with matplotlib.pyplot.annotate() when I use big numbers in the horizontal axis, for example doing time series with time data in "seconds since epoch" where the data will reach 10^9.

Here is an example:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

import os.path

import random

import calendar

save_path='my_path'

fig,ax=plt.subplots(2,sharex=True)
fig.set_size_inches(6,5)

a=int(calendar.timegm((2009,1,1,0,0,0)))
b=int(calendar.timegm((2009,2,1,0,0,0)))
x=xrange(a,b,(b-a)/100)
#x=xrange(0,b-a,(b-a)/100)

y=[random.random() for i in x]
z=[random.random() for i in x]

ax[0].scatter(x,y)
ax[1].scatter(x,z)

for sub in ax:
    sub.set_xlim(x[0],x[-1])

ax[0].annotate('test',(0.1,0.1),textcoords='axes fraction')
ax[1].annotate('test',(0.9,0.9),textcoords='axes fraction')

fig.savefig(os.path.join(save_path,'test.png'),bbox_inches='tight')
plt.close()

With x=xrange(a,b,(b-a)/100) I get: enter image description here

While with x=xrange(0,b-a,(b-a)/100) I get: enter image description here

I don't understand why the first case doesn't work but the second case works as expected, I just reduced the numbers basically. I have no problems if I use 'data' coordinates though.

0

There are 0 answers