How to match colors of an extracted contour with the colors of the main figure?

I extracted the region for activity, xi = linspace(58, 115) and l=(1.24787e-06, 2.42592e-06) from the figure attached, which is represened by the red and yellow colors. Please how can I set colormap colors for the extracted figure to be the same colors (red and yellow) as the figure main(attached figure)? The code for the main plot is given below:
figure;
[C,h] = contourf(L, xi, intul, [min(min(intul)):0.05e-11:max(max(intul))],'ShowText','off','edgecolor','none');
c = colorbar;
colormap jet
clabel(C,h)
hold on
for i=1:length(L)
for j=1:length(xi)
plot(L(i), xi(j), 'w.', 'LineWidth', 2, 'MarkerSize', 5)
end
end
set(gca,'clim',[min(min(intul)) max(max(intul))]);

 Accepted Answer

colormaps by default cover the range of data in your figure. You will want to manually set the color limits in the extracted figure to match the original figure so that the color is scaled the same.
In the original figure, extract the limits using lims = clim
In the new figure, apply the same limits using clim(limits)
Z = peaks;
[M,c]=contourf(Z);
colormap jet
colorbar
hold on
rectangle('Position',[17 31 15 15])
hold off
lims = clim
lims = 1×2
-6.5466 8.0000
If you do not modify clim, the colors still go from dark blue to dark red
figure
contourf(Z(31:46,17:32))
colormap jet
colorbar
Set clim so the color range in the cropped figure is the same as the original.
figure
contourf(Z(31:46,17:32))
colormap jet
clim(lims)
colorbar
You will notice that, unless otherwise specified, contourf also sets the number of contour levels automatically. Use the properties of the original contour plot to apply the same levels here.
figure
contourf(Z(31:46,17:32),c.LevelList)
colormap jet
clim(lims)
colorbar

6 Comments

Thank you so much Cris, you explain is very clear.
Edited: corrected name of variable, missing linspace for calculating L
Sorry, I had to come again Cris. I tried:
load sample.mat
intul = intabsur;
figure;
xi = linspace(0, 120,20);
and
L=linspace(2e-08, 6.66666666666667e-06, 20);
[C,h] = contourf(L, xi, intul, [min(min(intul)):0.05e-11:max(max(intul))],'ShowText','off','edgecolor','none');
c = colorbar;
colormap jet
clabel(C,h)
hold on
for i=1:length(L)
for j=1:length(xi)
plot(L(i), xi(j), 'w.', 'LineWidth', 2, 'MarkerSize', 5)
end
end
% Set contour limit
lims = clim;
% Figure around the isolated maximum
fig6 = figure(6);
contourf(intul(6.63544e-11:7.55125e-11, 1.12279e-10:1.34579e-10),c.LevelList)
Warning: Integer operands are required for colon operator when used as index.
Index in position 1 is invalid. Array indices must be positive integers or logical values.
colormap jet
clim(lims)
colorbar
But I recieved: "Index in position 1 is invalid. Array indices must be positive integers or logical values." I have attacehd my data.
When indexing, you must use integer values (rows and columns), which may not be the displayed values. Notice my example used Z(31:46,17:32)
Your code is using intul(6.63544e-11:7.55125e-11, 1.12279e-10:1.34579e-10)
As an aside, note that when you do not specify an interval when using the colon operator, 1 is used. Given the scale of your values, you will only get a single value. Again, this will not fix the error message.
x1 = 6.63544e-11:7.55125e-11
x1 = 6.6354e-11
% with an appropriately scaled increment specified
x2 = 6.63544e-11:0.1e-11:7.55125e-11
x2 = 1×10
1.0e-10 * 0.6635 0.6735 0.6835 0.6935 0.7035 0.7135 0.7235 0.7335 0.7435 0.7535
Thank you Cris . I have modified the code now. Is working but I still can't find appropriate indices around the region I want extract for the matrix ulint. I tried "find" function but is returning an empty array.
ul1 = load('sample1.mat');
ulint=ul1.intul;
xi = linspace(0, 120,20);
L=linspace(2e-08, 6.66666666666667e-06, 20);
figure
[C,h] = contourf(L, xi, ulint, [min(min(ulint)):0.05e-11:max(max(ulint))],'ShowText','off','edgecolor','none');
c = colorbar;
colormap jet
clabel(C,h)
hold on
for i=1:length(L)
for j=1:length(xi)
plot(L(i), xi(j), 'w.', 'LineWidth', 2, 'MarkerSize', 5)
end
end
lims = clim;
% Figure around the isolated maximum
fig6 = figure(6);
contourf(ulint(9:18, 3:10),h.LevelList)
colormap jet
clim(lims)
colorbar
This is just a rough index. How can I locate the appropriate indices?
I would use logical indexing of xi and L. You need to define both rows and columns. Note in the original sample you shared, it appears your Y values are in the wrong scale: intul(6.63544e-11:7.55125e-11, 1.12279e-10:1.34579e-10)
load sample1.mat
figure;
xi = linspace(0, 120,20);
L=linspace(2e-08, 6.66666666666667e-06, 20);
[C,h] = contourf(L, xi, intul, [min(min(intul)):0.05e-11:max(max(intul))],'ShowText','off','edgecolor','none');
c = colorbar;
colormap jet
clabel(C,h)
hold on
[XX,YY] = meshgrid(L,xi);
plot(XX,YY,'w.','MarkerSize',5)
rectangle('Position',[0.5e-6 50 2.5e-6 60])
hold off
% Capture color limit
lims = clim;
% Figure around the isolated maximum
fig6 = figure(6);
Ln = L(L>=0.5e-6 & L<=3e-6);
xin = xi(xi>=50 & xi<=110);
% for an array, rows = y, cols = x
contourf(Ln,xin,intul(xi>=50 & xi<=110,L>=0.5e-6 & L<=3e-6),h.LevelList,'ShowText','off','edgecolor','none')
colormap jet
clim(lims)
colorbar
There is a little difference here because the actual values chosen do not exactly align with the value in L and xi.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!