Common color range for surface.m and surf.m for subplots
3 views (last 30 days)
Show older comments
Hello,
I would like to plot a 2D matrix where rows and columns are coordinates and the cell value is assigned a specific color. My data is a bit more complex, but to illustrate I made some simple code plotting a sine wave.
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
% 2D figure
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
% 3D figure
figure;
subplot(1,2,1);
surf(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
subplot(1,2,2);
surf(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
As you can see, one figure shows the sine waves as a 2D, one as a 3D figure. My problem is: I would like one given color to represent the same value in both subplots. As you can see, the amplitude in the second subplot is twice as high, but the colors used are the same!
My second problem is: I instead of continous colors, I would like to use categorical colors. For example, yellow for 0.2 < z <= 0.4 and green for 0.4 < z <= 0.6. The categories and the border between them should then be identical in both subplots. Please note that this means more categories will be required for subplot 2 as the range is larger.
It would be great if someone could help me with one or both of these problems!
Thanks,
Toby
0 Comments
Accepted Answer
Ameer Hamza
on 17 Apr 2020
Edited: Ameer Hamza
on 17 Apr 2020
The first problem is quite simple. You just need to specify the limit of the color axis. If you set the same limits for all axes, then, value at the same level will have the same color. Try this code
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
% 2D figure
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]) % <---- this line
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]) % <---- this line
% 3D figure
figure;
subplot(1,2,1);
surf(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]) % <---- this line
subplot(1,2,2);
surf(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]) % <---- this line
The second problem is a bit complicated. You need to create your own colormap such that the color appears to be discrete. Try the following code. I divided the interval [0,2] into 5 portions. This code used random colors for each portion, you can define your own RGB colors equal to the number of portions.
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
% define the custom discrete colormap
color_edges = [0 0.4 0.8 1.2 1.6 2.0]; % 5 intervals
colors = rand(5,3); % define as many rgb colors as the number of intervals
resolution_colormap = 100;
t = linspace(min(color_edges), max(color_edges), resolution_colormap);
c_idx = discretize(t, color_edges);
c_map = colors(c_idx, :);
% 2D figure
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
% 3D figure
figure;
subplot(1,2,1);
surf(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
subplot(1,2,2);
surf(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
colormap(c_map);
caxis([0 2])
2 Comments
More Answers (0)
See Also
Categories
Find more on Orange in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!