#!/usr/bin/env python3
#
'''
.. module:: xcorplot
:synopsis: Plot crosscorrelation plots as images for speedup of pdf
.. moduleauthor:: Cor de Vries <c.p.de.vries@sron.nl>
'''
import numpy
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
colors={}
colors['blue']=numpy.array([0,0,255])
colors['red']=numpy.array([255,0,0])
colors['green']=numpy.array([0,255,0])
[docs]def xcorplot(pp,x,y,color='blue',lfact=0.03,res=320):
'''
plot crosscorrelation plots as images for speedup of pdf
Args:
* `pp` = plot instance for plot
* `x` = data points x-coordinates
* `y` = data points y-coordinates
Kwargs:
* `color` = color for data points ('green'|'red'|'blue')
* `lfact` = enlargement factor for plot axes
* `res` = correlation resolution (sets number of pixels)
'''
cc=colors[color]
xmin=numpy.amin(x) # scaling of datapoints to image
xmax=numpy.amax(x)
dx=xmax-xmin
xmin=xmin-lfact*dx
xmax=xmax+lfact*dx
dx=dx+2.0*lfact*dx
ymin=numpy.amin(y)
ymax=numpy.amax(y)
dy=ymax-ymin
ymin=ymin-lfact*dy
ymax=ymax+lfact*dy
dy=dy+2.0*lfact*dy
if dx == 0.0:
dx=1.0
if dy == 0.0:
dy=1.0
_, _, w, h=pp.get_position().bounds # dimensions of plot space
nx=res # number of pixels in x-direction
ny=int(float(h)/float(w)*nx+0.5) # number of pixels in y-direction
image=numpy.zeros((ny,nx,3),dtype=numpy.uint8) # plot-image
image[:,:,:]=255 # set all image pixels to white
ix=((x-xmin)/dx*(nx-1)+0.5).astype(int) # image coordinates for points
iy=-((y-ymin)/dy*(ny-1)+0.5).astype(int)
image[iy,ix,:]=cc # fill the image
pp.imshow(image,extent=[xmin,xmax,ymin,ymax],\
aspect='auto',interpolation='none') # plot the image
return
# ================================================================
if __name__ == "__main__":
pdf=PdfPages('testpdf.pdf')
nn=50000
# nn=10
x=numpy.random.random(nn)*1000.0
y=numpy.random.normal(0.0,1.0,nn)*10.0
np=4
f,ax=plt.subplots(np,1,squeeze=False)
for i in numpy.arange(np):
xcorplot(ax[i,0],x,y,color='blue')
# ax[i,0].scatter(x,y,color='blue')
fpage=plt.gcf()
fpage.set_size_inches(8.27, 10.75)
plt.tight_layout()
plt.savefig(pdf,format='pdf',papertype='a4')
f,ax=plt.subplots(np,1,squeeze=False)
for i in numpy.arange(np):
xcorplot(ax[i,0],x,y,color='green')
ax[i,0].plot([x.max(),x.min()],[y.max(),y.min()],'r-')
# ax[i,0].scatter(x,y,color='blue')
fpage=plt.gcf()
fpage.set_size_inches(8.27, 10.75)
plt.tight_layout()
plt.savefig(pdf,format='pdf',papertype='a4')
# plt.show()
pdf.close()
plt.close('all')