How to paint a surface with higher contrast

In Matlab, I am trying to build a function on a two-dimensional Euclidean space with the following code

s=.05; x=[-2:s:2+s]; y=[-1:s:3+s]; [X,Y]=meshgrid(x,y); Z=(1.-X).^2 + 100.*(YX.*X).^2; surf(X,Y,Z) colormap jet 

Here's what my plot looks like:

enter image description here

I want to paint the surface with greater contrast, just as Wikipedia shows enter image description here

The Wikipedia plot is drawn with Python code:

 from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.colors import LogNorm import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = Axes3D(fig, azim = -128, elev = 43) s = .05 X = np.arange(-2, 2.+s, s) Y = np.arange(-1, 3.+s, s) X, Y = np.meshgrid(X, Y) Z = (1.-X)**2 + 100.*(YX*X)**2 ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, norm = LogNorm(), cmap = cm.jet) plt.xlabel("x") plt.ylabel("y") plt.show() 

My Matlab code and the Wikipedia Python code seem to use β€œjet” as the color map, but their actual mappings of the height value for the color are different. So I was wondering how can I get a similar coloration in Matlab?

Thank you and welcome!

+6
python matlab plot
source share
2 answers

You can achieve a similar appearance:

Here you can change your code:

 s = .05; x = [-2:s:2+s]; y = [-1:s:3+s]; [X, Y] = meshgrid(x, y); Z = (1.-X).^2 + 100.*(YX.*X).^2; minZ = min(Z(:)); % Find minimum value of Z maxZ = max(Z(:)); % Find maximum value of Z C = minZ+(maxZ-minZ).*log(1+Z-minZ)./log(1+maxZ-minZ); % Create a log-scaled % set of color data surf(X, Y, Z, C, 'EdgeColor', 'none'); colormap jet 

And here is the result:

enter image description here


How log scaling works ...

Z scaling data, which is used to create C color data, causes the red-orange range of the color map of the jet to be used by more shallow dots, improving the contrast for that particular surface. How this works can be visualized with this simple example:

 x = 0:5:100; % Create a range of values from 0 to 100 plot(x, x, 'b-*'); % Plot the values as a straight line (y = x) in blue hold on; % Add to the plot plot(x, 100.*log(1+x)./log(101), 'r-*'); % Plot a log-scaled version of x in red colorbar % Display the default jet color map, for comparison 

enter image description here

The source blue dots are evenly distributed over the range of colors that they correspond to in the color bar on the right. When scaling along the scale, these points shift up to the red line. Notice how this leads to a decrease in the density of dots in the lower blue-green range and an increase in the density of dots in the red-orange range.


Getting better contrast overall ...

For the specific surface used here, the log scaling of color data helps to use a larger range of the color map at all points on the surface. Since there are many dots at a lower height (that is, a color index), the logarithm scaling extends to these low values ​​to use a wider range of colors in a large surface trough.

However, if you want to improve contrast for an arbitrary surface by improving the use of the range of your color map, scaling the log will not always work. A general solution that may work better is to sort all the height values ​​for your surface in ascending order, and then match them with a linear range that spans your entire color map. Here is what you could get if you did it for your surface:

 C = Z; [~, index] = sort(C(:)); C(index) = 1:numel(index); h = surf(X, Y, Z, C, 'EdgeColor', 'none'); colormap jet caxis([1 numel(index)]); 

enter image description here

This usually contrasts better than the default coloration C = Z

+7
source share

Exit the man page for colormap:

Files in a color folder generate several color palettes. Each file takes a colormap size as an argument. For example,

Colormap (HSV (128)) creates an hsv color package with 128 colors. If you do not specify a size, a color palette of the same size as the current color palette will be created.

I used this function to change the range of colors to make better use of the available spectrum.

+1
source share

All Articles