Monday, April 4, 2011

Transparent figures in Matlab

If you want to make an overlay between two images in a program like keynote, powerpoint, pages, illustrator and want the images to be transparent, how would you achieve the transparency? Being a heavy Matlab user, I recently had to export my Matlab data, in the form of two spectra, with a transparent background. 

A quick google query showed that I was not alone with having this question. I found as many answers as there were questions, but finally there was one which actually worked for me. 

The excellent package export_fig can be downloaded from the Matlab central. With export_fig, making a transparent background comes down to three (!) simple steps.

  • Make the image as you want it to look 
  • Set the background of the current figure and axes to none
  • set(gcf, 'color', 'none); set(gca, 'color', 'none');  
  • Export the figure to a png (this works well, eps or pdf might work too)
  • export_fig figname.png
  • This png file is now transparent.
An example of such a figure is shown below (for the interested it shows two molecular transitions from the CO molecule in a faraway galaxy, but I won't tell you which ! ). The blogger style seems to make the background slightly grey, but the png is really transparent. More Matlab tips are coming!



No comments:

Post a Comment