merging two axes from two different figures (Tk canvas) into one

Dear all,

I wrote an application allowing to display an imshow image in one part of a Tk GUI and the corresponding colorbar in another part. This allows me to work on the image with more freedom. My problem is then to save the result which implies getting the 2 axes into another figure. I’ve been fighting for some time but I can’t get it right. The problem in the end is that I don’t really understand the exact link between: figure <-> canvas <-> axes. I guessed that one creates a Figurecanvas which is the GUI link where to put the figure which itself contains the axes. But when I tried to get the axes and put them into a new figure, they don’t appear…

Can anyone give me a hint on what I did wrong ? I’m using matplotlib v.0.90.1

The code looks something
like:

···

########## init part ###########
import matplotlib as mpl
mpl.use(‘TkAgg’)
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

########## creating GUI elements ###########
#create figures
self.mplfImageDisplay = mpl.figure.Figure(figsize=(5,4),dpi=100,facecolor=‘white’)
self.mplfImageColorbar = mpl.figure.Figure(figsize=(0.7,4),dpi=100,facecolor=‘white’)

#add tk.DrawingArea
self.cvImageDisplay = FigureCanvasTkAgg(self.mplfImageDisplay,master=leftPaneImage)
self.cvImageDisplay.get_tk_widget().pack()
self.cvImageColorbar = FigureCanvasTkAgg(self.mplfImageColorbar,master=centerPaneImage)
self.cvImageColorbar.get_tk_widget().pack()

#pack all in main frame
self.cvImageDisplay._tkcanvas.pack()
self.cvImageColorbar._tkcanvas.pack()

########## adding axes to GUI elements ###########
#get data (2d
matrix)
self.finaldata = self.build_data()

#define first axes (image)
self.axes1 = self.mplfImageDisplay.gca(frameon=False)
self.axes1.set_position((0.015,0.02,0.98,0.96))
display = self.axes1.imshow(self.finaldata,
origin=‘upper’,

aspect=‘auto’,
interpolation=interpolation,
cmap=imagecolormap,

vmin=minvalue,
vmax=maxvalue)
self.axes1.set_axis_off()

#define colorbar
self.axes2 = self.mplfImageColorbar.gca()
self.axes2.set_position((0.1,0.05,barsize,0.9))
self.colorbar = figcolorbar.colorbar(display,

format=’%’+legendformat,
cax=self.axes2)
#set font size
colorbartextlist = self.axes2.get_yticklabels()
for element in colorbartextlist: element.set_fontsize(fontsize)

#display image
self.cvimage.show()
cvcolorbar.show()

########## saving result ###########
#get current figure
image = self.mplfImageDisplay.gca()
colorbar = self.mplfImageColorbar.gca()

#calculate fig width
imagewidth = self.mplfImageDisplay.get_figwidth()
colorbarwidth = self.mplfImageColorbar.get_figwidth()
totalwidth = imagewidth + colorbarwidth

#calculate fig
height
imageheight = self.mplfImageDisplay.get_figheight()
colorbarheight = self.mplfImageColorbar.get_figheight()
totalheight = max(imageheight,colorbarheight)

#create new figure
outputfigure = mpl.figure.Figure(figsize=(totalwidth,totalheight),dpi=100,facecolor=‘white’)

#add axes
outputfigure.add_axes(axes=image,anchor=‘w’)

outputfigure.add_axes(axes=colorbar,anchor=‘e’)

#I also tried playing about adding a canvas but it didn’t help much
#cvouputfigure = FigureCanvasTkAgg(outputfigure)
#outputfigure.draw(cvouputfigure.get_renderer())

#save figure
outputfigure.savefig(outputfilename)


Ne gardez plus qu’une seule adresse mail ! Copiez vos mails vers Yahoo! Mail