Hi,
today I urgently needed to plot a matrix in a given
region of a plot. It seems that imshow does not
support this (at least I could not find how)
and I don't understand the internals well
enough to add such a feature.
As work-around I have set up the quick hack listed
at the end of this mail.
It is not really complicated, but
maybe it is useful to someone else.
Note, that there are a couple of problems:
- zooming does not work as expected
(the displayed matrix stays fixed in absolute coords wrt to
the current graph)
- drawing a line over the shown bitmap is not possible
- presumably more
Is there a way to this better with imshow?
Best,
Arnd
···
#########################
from pylab import *
def plot_mat(mat,extent=None):
""" Plot a matrix (or whatever suits imshow) at
the place defined by extent=[x0,x1,y0,y1].
"""
g=gca()
l,b,w,h=g.get_position()
x0,x1=g.get_xlim()
y0,y1=g.get_ylim()
#print "Axes coords of curr axes:",l,b,w,h
#print "Ranges:",x0,x1,y0,y1
if extent!=None:
inset_x0=extent[0]
inset_x1=extent[1]
inset_y0=extent[2]
inset_y1=extent[3]
else:
inset_x0=x0
inset_x1=x1
inset_y0=y0
inset_y1=y1
#print "WANTED:",inset_x0,inset_x1,inset_y0,inset_y1
inset_w=w*(inset_x1-inset_x0)/(x1-x0)
inset_h=h*(inset_y1-inset_y0)/(y1-y0)
inset_l=l+(inset_x0-x0)/(x1-x0)*w
inset_b=b+(inset_y0-y0)/(y1-y0)*h
#print "RESULT:",inset_l , inset_b , inset_w , inset_h
ax=axes( [inset_l , inset_b , inset_w , inset_h],axisbg="y")
setp(ax,xticks=[],yticks=[])
imshow(arr,interpolation="nearest")
# 2D
arr=reshape(arange(25),(5,5))
# 1D plot
x=arange(2.0,10.0,0.1)
line1,=plot(x,x**2)
line2,=plot(x,x**2)
#draw()
plot_mat(arr,extent=[5.0,8.0,15.0,50.0])
# Problem: try to plot this over the other plot,
# does not work
setp(line2,data=(x,0.25*x**3),zorder=1000)
show()