Common color range for surface.m and surf.m for subplots

3 vues (au cours des 30 derniers jours)
T FW
T FW le 17 Avr 2020
Commenté : Ameer Hamza le 18 Avr 2020
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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 17 Avr 2020
Modifié(e) : Ameer Hamza le 17 Avr 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 commentaires
T FW
T FW le 18 Avr 2020
Hi Ameer,
thanks for th quick reply. It works like a charm. Using a discretized colormap seems to do the trick. I found it helpful for the 3D plot to add...
zlim([0 2]);
...in order to make the z values (height) represent the same number in the subplot.
I played around a bit and this simplified version does seem to do the same as your code:
numCats = 10; % number of categories
[X,Y] = meshgrid(linspace(-1/2*pi,1/2*pi));
Z1 = cos(X).*cos(Y);
Z2 = 2*cos(X).*cos(Y);
% define color map
cmap = viridis;
cmap = cmap(1:floor(length(cmap)/numCats):length(cmap),:);
% 2D figure
figure;
subplot(1,2,1);
surface(X,Y,Z1,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]);
colormap(cmap);
subplot(1,2,2);
surface(X,Y,Z2,'EdgeColor','flat','LineStyle','none','FaceColor','flat');
caxis([0 2]);
colormap(cmap);
Do you see any problem with it? It basically reduced the number of rows in the colormap to the number of required categories.
Thanks,
Toby
Ameer Hamza
Ameer Hamza le 18 Avr 2020
I am glad to be of help. Your code also seems correct.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Orange dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by