File Exchange

image thumbnail

setFigTransparency - set figure transparency / fading

version (3.82 KB) by Yair Altman
setFigTransparency sets the transparency/opacity of a figure window, with optional fading effect


Updated 13 Oct 2011

View Version History

View License


oldAlpha = setFigTransparency(hFig, alpha, fadeDuration, blockingFlag)

setFigTransparency sets the figure hFig's transparency value. The entire figure window, including all internal menus, toolbars and components, is made transparent according to the alpha value.

oldAlpha = setFigTransparency(...) returns the old transparency value of the specified figure, prior to its modification.

This submission is based on an original idea implemented by Malcolm Lidierth in his MUtilities submission:

Input parameters: (all parameters are optional)

hFig (default=gcf) - Handle(s) of the modified figure(s). If component handle(s) is/are specified, the containing figure(s) will be inferred and used.

alpha (default=0.5) - Transparency value, between 0.0 (=fully transparent) and 1.0 (=fully opaque). Note that all Matlab figure windows are created opaque. alpha<0 indicates that alpha value should not be modified.

fadeDuration (default=0) - Number of seconds for fade-in/fade-out effect. Note: default value of 0 means immediately (no fading)

blockingFlag - (default=true) Whether or not the function should wait for the fade-in/fade-out effect to complete before returning

oldAlpha = setFigTransparency(hFig,-1); % get hFig's current alpha
oldAlpha = setFigTransparency(hFig); % set hFig's alpha to 0.5 (semi-transparent)
oldAlpha = setFigTransparency(hFig,0.7); % set hFig's alpha to 0.7
oldAlpha = setFigTransparency([hFig1,hFig2],0.7); % set transparency for several figures
oldAlpha = setFigTransparency(hFig,0.3,1.5,false); % non-blocking fade over 1.5 secs

Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7.9 (R2009b) and higher, but use at your own risk!

Cite As

Yair Altman (2021). setFigTransparency - set figure transparency / fading (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Yair Altman

IMPORTANT NOTE: this utility DOES NOT WORK on R2013b/Java7 and higher, only on earlier releases. The failure is due to a change in the way that transparency works in Java 7 compared to earlier releases. If I ever find a solution, I will post it here. In the meantime, please do not comment about this issue here.


I think this function is fantastic and I want to keep it useful, I am on a MAC and Using Matlab (R2013a), the line

figure(2);plot([0 0],[1 1]);shg;setFigTransparency(gcf)

works fine; however Using Matlab (R2013b), with Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode, the same line produces an error

figure(2);plot([0 0],[1 1]);shg;setFigTransparency(gcf)
Error using setFigTransparency (line 137)
Java exception occurred:
java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(
at java.awt.Window$1.setOpacity(
at com.sun.awt.AWTUtilities.setWindowOpacity(

which prevents the figure from becoming transparent.

Below is some standalone code which when pasted in the commandwindow will quickly show the problem

figure(2);set(gcf,'color',[1 0 0]);
plot([0 0],[1 1]);

[lastWarnMsg lastWarnId] = lastwarn;
warning(oldJFWarning.state, 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
lastwarn(lastWarnMsg, lastWarnId);
jWindow = jFigPeer.fHG1Client.getWindow;
newAlpha = .5;

% throws the following exception in (R2013b) and does not in (R2013a)
% Java exception occurred:
% java.awt.IllegalComponentStateException: The frame is decorated
% at java.awt.Frame.setOpacity(
% at java.awt.Window$1.setOpacity(
% at com.sun.awt.AWTUtilities.setWindowOpacity(


Are there any workarounds?





Yair Altman

@Razvan - setFigTansparency cannot work on an invisible figure for technical reasons. However, you can create a *Visible* figure with initial position outside the screen boundaries, which is effectively hidden. This works nicely (at least for non-docked figures):

hFig = figure('Visible', 'off');
pos = get(hFig,'Position');
set(hFig, 'Position',[-100,-100,1,1], 'Visible','on');
setFigTransparency(hFig, 0);
set(hFig, 'Position',pos);
setFigTransparency(hFig, 1,1);


This function is very nice!

I couldn't figure out though how I can fade in a figure. I tried to set the 'visible' property to 'off' first then set alpha to 0, but this doesn't work.

The only option that I found is something like this:

hFig = figure('Visible', 'off');
set(hFig, 'Visible', 'on')
setFigTransparency(hFig, 0);
setFigTransparency(hFig, 1, 1);

but it doesn't look nice because the figure pops up before it disappears and fades in again...
Any advise?




Thank you for the fix.

Yair Altman

@Jveer - I uploaded a fix for R2011b. It should be available here shortly. Note that your method of attracting my attention is entirely unprofessional.



This doesn't work with R2011b

I get the following error:

Error using setFigTransparency>getJFrame (line 194)
Cannot retrieve the Java Frame reference for figure 174.0021

Error in setFigTransparency (line 105)
jff = getJFrame(hFigCell{figIdx});

Error in MPMSimPre>TF_Callback (line 1536)

So I re-rate.

Please advise.


After I installed JRE 1.6.0_24, the UITREE objects of Matlab 2009a cannot be reliably resized by "set(TreeH, 'Position', Pos)": Sometimes it works, sometimes the update appears after some seconds only, or not at all. I'm absolutely sure, that Yair could find a solution, but this is actually not his job. It would be nice if TMW offers a long-term supported MATLAB release which accepts updated JREs seamingless.
Now I'm using the Windows-API to enable alpha-blending. This has the additional feature, that I can create a full size window (not just maximized, but all window borders outside the screen) and a stencil color to let the background disappear. This allows to create plots transparently "in" the command window. Ugly. Does this work with the Java methods also?
I rate this with 5 stars, but I will not use it with a retrofit JRE.

Yair Altman

Note: a retrofit of JRE means installing JRE 1.6.0_10 (also called "Java 6 update 10"), or any later version (e.g., the latest available version today is 1.6 update 24). The JRE can be downloaded from and you can configure Matlab to use it according to the instructions in

All of this is only necessary in Matlab releases R2009a (7.8) and earlier, which pre-bundle an earlier JRE version that does not support window frame transparency/opacity used in this submission.

Matlab R2009b (7.9) and onward, at least on Microsoft Windows, pre-bundle a JRE version that does support transparency/opacity and so do not require a retrofit.



MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!