When I create a display using pcolor of data that is contained within a circle I insert NaN values in all locations not within the circle and then the display creates white areas where the NaNs are located. Is there any way I can get this same effect but have the surround black instead of whiite?

 Accepted Answer

DGM
DGM on 17 Jun 2022
Edited: DGM on 18 Jun 2022
When you mask off data using NaN, the plot is simply not drawn there, revealing whatever graphics objects might be beneath it. If there aren't other objects beneath it in the axes, the color that's shown in that region is the axes background color, and is independent of the colormap that's in use. Just set the color of the axes to whatever color you want.
numpoints = [1000 1000]; % [x y]
% build boundary curve
x = linspace(-2,2,numpoints(1));
y = 4*x-x.^3;
% build 2D map
y2 = linspace(-4,4,numpoints(2)).';
z = sin(x*2).*sin(y2*2);
% set unwanted map data to NaN
z(y2>y) = NaN;
% plot it
pcolor(x,y2,z)
colormap(parula)
shading flat
set(gca,'color','m') % magenta
% or maybe a different color
figure
pcolor(x,y2,z)
colormap(parula)
shading flat
set(gca,'color','k') % black

6 Comments

The original answer was good. I am not sure what has changed in the version but it is a good plan in any case.
DGM
DGM on 19 Jun 2022
Edited: DGM on 19 Jun 2022
I just changed some of the explanation to be a bit more generalized. It was not strictly true that the NaN areas show the axes background color. More generally speaking, it shows whatever is beneath the pcolor() object in the stack. The difference is likely not relevant for your usage, but it might be for the next person who comes along.
This is what I like to see in a person giving advice in technical matter, being sure that the details are correct and correcting the explanation if this is found to not quite the case. This is especially true for software programs where what might seem to be minor details can often be quite important it getting thing to work right or where understanding the details can lead to using the software in creatative ways.
I would like to know what a 'stack' means for MatLab coding and how you can tell what is in a 'stack'
Think of an axes having, or being able to have, a bunch of images on top of each other in a stack. Each image is a layer. If you've ever used Photoshop you're familiar with layers where images are stacked on top of each other vertically. And you can specify the order of the layers (what's on top of what). It's like that.
DGM
DGM on 19 Jun 2022
Edited: DGM on 19 Jun 2022
What @Image Analyst said is basically the analogy I'd give. Objects in the figure window are ultimately reduced to raster images that are composited just as you might combine transparent images in an image manipulation environment or how one might stack multiple cels in traditional animation.
As far as how to tell what's in the "stack", that's kind of hard to say. On one hand, you can look at the 'children' property of the axes or figure to see what objects are there, but it might not always be clear whether those objects are relevant to the discussion. For example, it's possible to position objects (e.g. text objects) outside the extent of their parent axes. While it's possible that objects within the axes can be made invisible, certain visible objects may have their handles hidden, preventing them from showing up when checking the descendants of their assumed parent.
As confusing as that sounds, it's usually pretty straightforward. There are usually only a few major objects in the axes, and you'll know what they are because you wrote the code. Their stacking order is usually apparent based on the order they were created. The stacking order of an object can be manipulated with uistack().

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 15 Jun 2022
Why not just write 0's to those pixels instead of NaNs?

4 Comments

If the data, is it often does, contains valid 0's then assigning the areas outside to be 0 will not work as real 0 will have a color other than black.
Why would that be? Do you have an indexed image? If so just assign the value to the value in the colormap that is black.
Consider the fact that the height in a surface, referenced to a given point which could be the center of the surface, with have values above and below this value. Perhaps you have not thought of this. In the meantime I have found a solution which involves creating a colormap with black as the most negative value and creating a spacing vector for a contourf map that has the lowest value just one increment less than the most negative value of data. I then add a mask to the data with 0's where the data actually exits and at other location in the mesh value one increment below the most negative data value. This will then create the desired black surround and preserve the colored contourf map where there is real data.
I did think of that, or something like that. It really doesn't matter if the matrix values represent an intensity like a regular gray scale image or a height or elevation. I was thinking values were brightness but they just as well could have been elevation. That's why I suggested using a colormap and it looks like you found a working solution using that. You should also look at the function clim (r2022a or later) or caxis (r2021b or earlier).
But @DGM presented an alternative way and it looks like you preferred that way since you accepted the answer.

Sign in to comment.

Products

Release

R2022a

Asked:

on 15 Jun 2022

Edited:

DGM
on 19 Jun 2022

Community Treasure Hunt

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

Start Hunting!