MATLAB Answers

Colormap overriden by new Parent figure

16 views (last 30 days)
I've encountered a strange problem with colormaps. When I transfer the "Parent" attribute of my axis to a new figure, then its colormap is overriden. Here's example code:
f1 = figure;
% At this point, the colormap should be "gray"
f2 = figure;
f1.Children.Parent = f2;
% colormap is now "parula" after the image axes is transferred to figure 2
f2.Children.Parent = f1;
% image is now "gray" again
Bug?? Generalized workarounds??
Obviously, I can reset the colormap after transfer, but it its troublesome with multiple axes with multiple colormaps, also with axis as children of panels (multilevel UI objects).
This is Matlab R2018a, Win 10.

  1 Comment

Spencer Chen
Spencer Chen on 11 Apr 2019
I have finally got to the bottom of this colormap problem. The problem wasn't in the reassignment parent, nor with the GUILayoutToolbox. It was actually to do with preview() of the Image Acquisition Toolbox.
Executing preview() somehow touches all my colormap settings. On screen, my colormaps get set to 'gray'. On save the panels, these images are saved as 'parula'.
Solution: Resetting all my axes to the correct colormap after executing preview() solves the problem.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jan 2019
By default colormap() sets the figure colormap, not the axes colormap. You are moving the axes to a different figure that has a different (default) figure colormap active so the axes takes on that other colormap.
The solution is
colormap(gca, gray)


Sign in to comment.

More Answers (3)

Spencer Chen
Spencer Chen on 17 Jan 2019
Thanks. I forgot about that.
This works for my example.
However, for some reason I'm still having trouble with colormap with multi-level UI objects when I transfer between figures.
Let me debug further.


Sign in to comment.

Spencer Chen
Spencer Chen on 17 Jan 2019
There must be a flag somewhere so the system knows whether to override the axes colormap with the figure colormap. I cannot find this flag (in the axes properties).
My problem is intermittent, so I cannot reduce it to simple code for now. The long description is to do with saving subplots from my GUI. I have various panels/tabs with subplots that I want to save to individual file. The way I do that is create a new figure, temporarily assign the parent of the Panel to the new figure, then save the figure, after which I assign the Panel back to its original parent.
One thing for sure, I've set the colormap of the axes. Initially it all works fine. I collect data, then save the plots, and the colormaps of the saved figures are correct. But I suspect at some point in using my GUI, the flag that marks the axis as having its own colormap is reset by an internal routine, causing it to switch colormap map when I assign it to a new figure. Once the colormap switch occured, then subsequent saves of my plotted data all have the default parula colormap.
Restarting my GUI resets the colormap back to what I want.
I'm using GUI Layout Toolbox, which may be contributing to this problem. And running my GUI on R2016a works fine. R2018a is switching my colormaps.


Walter Roberson
Walter Roberson on 18 Jan 2019
You should consider copyobj to the figure instead of setting the parent.
Are you using imshow() ? If so it might be deleting your axes. I recommend against using imshow() for anything other than interactive exploration or programs that are nearly throw-away.
Spencer Chen
Spencer Chen on 18 Jan 2019
Thanks. copyobj() was the first thing I tried when I put the GUI together, but something in the routine is cropping my copied panel (see attached). It was then that I found that temporary reassigning the Parent property to a new figure for saving my plots worked.
I'm using image() to create the image handle, then subsequently using CData to update the content. This way, the axes does not get redrawn and I achieve fast redraw on the GUI.
Here's my code when initializing one of my plots for your comment:
% init spatial time course
this.gui.plot.tc8x8bg = image(, [0 8], [0 8], ones(8));
set(,'Position',[0 0 1 1],'Unit','normalized');
And here's the code snippet for updating the
set(this.gui.plot.tc8x8bg, 'CData', img2, 'CDataMapping', 'scaled');

Sign in to comment.

Spencer Chen
Spencer Chen on 18 Jan 2019
Still working on this problem if anyone has more suggestions.
Attached is a screencap of my GUI with the panel on the left in the desired gray colormap. Also attached is the saved JPEG of this panel ending up with the parula colormap, using the following code:
function saveplot(obj, filename)
org.Parent = obj.Parent;
fig = figure('Visible','off','Position',[0 0 1120 840]);
% copyobj(obj, fig);
obj.Parent = fig;
obj.Parent = org.Parent;


Sign in to comment.




Community Treasure Hunt

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

Start Hunting!