Understanding the Color Matrix Input in surf()
20 次查看(过去 30 天)
显示 更早的评论
I'm trying to understand this code snippet:
[x,y] = meshgrid([-2:.2:2]);
Z = x.*exp(-x.^2-y.^2);
surf(x,y,Z,gradient(Z))
colorbar
Which is the example given in http://www.mathworks.com/help/matlab/visualize/coloring-mesh-and-surface-plots.html.
From the documentation for 'surf', it appears that 'gradient(Z)' is the color matrix. How is 'gradient(Z)' used to define the color? It's not entirely clear to me from the documentation for 'surf'. Does the colorbar indicate the value of 'Z', or 'gradient(Z)'?
0 个评论
回答(2 个)
Star Strider
2016-2-3
As I understand it, the Z value is used to determine the colour of the surface plot. The colorbar does not determine how the surface is coloured, but the colormap does. You can define the colormap from the available colormap options, or one you design on your own.
0 个评论
Mike Garrity
2016-2-3
编辑:Mike Garrity
2016-2-3
[FX,FY] = gradient(F) returns the numerical gradient of the
matrix F. FX corresponds to dF/dx, the differences in x (horizontal)
direction. FY corresponds to dF/dy, the differences in y (vertical)
direction. The spacing between points in each direction is assumed to
be one. When F is a vector, DF = gradient(F) is the 1-D gradient.
So, since we're only using the first return value, we're telling surf to use dF/dx for the color. If we look at the values:
dfdx=gradient(Z);
min(dfdx(:))
max(dfdx(:))
We see that dF/dx ranges from -0.0853 to 0.1922. Now, the surface is going to tell the axes what the range of the color data is. We can see that happen using the caxis function.
caxis
ans =
-0.0853 0.1922
If there were other objects in the axes which had color data, then these values would get stretched to include all of them.
Next, the surface will get a colormap from the axes. We can can see that colormap with the colormap function.
colormap
That returns a 64x3 array of values. The columns are the red, green, and blue values of 64 different colors. The surface is going to map the value -0.0853 to the first of those colors, and the value 0.1922 to the last of those colors. Values in between these two will choose colors from the 64 using linear interpolation.
The colorbar function shows a visual representation of how the axes is telling the surface to transform colors. You can see 64 different colors (actually kind of hard to see because the default colormap is quite smooth) and the values they map to.
On the other hand, we've told the surface object to use the array Z for the Z values. These range from -0.4218 to 0.4218. You can see this range on the Z ruler on the left side of the axes.
This shows that the function we're using for Z can be different from the function we're using for color. We could put other functions into the color data. For example, if I wanted to see the 1st derivative of Z with respect to y instead of x, I would do it like this:
[dfdx,dfdy] = gradient(Z);
surf(x,y,Z,dfdy)
colorbar
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172641/image.png)
Notice how the high values of df/dy (the yellow colors) are in the places were I would be going uphill if I was walking in the positive Y direction. And the low values of df/dy (the blue colors) are in places where I would be going downhill.
Or I could show the magnitude of the gradient like this:
surf(x,y,Z,sqrt(dfdx.^2 + dfdy.^2))
colorbar
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172642/image.png)
In this case, the large values are in areas where both df/dx and df/dy are large. Also notice in this case, that the bottom of the colormap is at 0 instead of being negative. That's because sqrt(dfdx.^2+dfdy.^2) will always be positive.
Another thing that's important to note here is that the surface is using a single color value for each of those little quadrilaterals. We can see that if we use a smaller, less smooth colormap.
colormap(lines(8))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172644/image.png)
That's because the default value of the surface's FaceColor property is 'flat'. We have another option, which is 'interp'. This will interpolate the color values across the interior of the quadrilaterals, as you can see here.
surf(x,y,Z,sqrt(dfdx.^2 + dfdy.^2),'FaceColor','interp')
colorbar
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172645/image.png)
Does that help?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!