MATLAB Answers

setting the limits for negative values for colormap

13 views (last 30 days)
Zahra Sheikhbahaee
Zahra Sheikhbahaee on 21 Jan 2021
Answered: Image Analyst on 21 Jan 2021
I am trying to set the limits for color in the image plot type by using this code
for g = 1:size(C,1)
subplot(6,1,g+4);
image(1 - C{g,1}), hold on
plot(O{g,1}(:),'.c','MarkerSize',16), hold on
if g < 2
title(sprintf('Outcomes and preferences - %s',MDP(1).label.modality{g}));
else
title(MDP(1).label.modality{g});
end
if g==size(C,1)
xlabel('trial');
end
xlim([1,size(MDP(1).C{g},2)*Nt]);
g_y=[0.5:1:size(MDP(1).C{g},1)+0.5]; % user defined grid Y [start:spaces:end]
g_x=[0.5:size(MDP(1).C{g},2):size(MDP(1).C{g},2)*Nt+0.5]; % user defined grid X [start:spaces:end]
for i=1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'b:','LineWidth',1.5) %y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'m:','LineWidth',0.75) %x grid lines
hold on
end
hold off
set(gca, 'XTick', (1 : size(MDP(1).C{g},2) : size(MDP(1).C{g},2)*Nt) );
set(gca,'XTickLabel',(1 : 1 : Nt));
set(gca,'YTick',1:numel(MDP(1).label.outcome{g}));
set(gca,'YTickLabel',MDP(1).label.outcome{g});
ax = gca;
ax.CLim = [min(C{g,1}(:,1)) max(C{g,1}(:,1))];
end
I got this error message
Error setting property 'CLim' of class 'Axes':
Value must be a 1x2 vector of numeric type in which the second element is larger than the first and may be Inf
The values of C{g,1} are negative and it caused that my figure gets dark so I wanted to define in advance the range of colormap but this error occured. Any suggestion?

  0 Comments

Sign in to comment.

Answers (2)

VBBV
VBBV on 21 Jan 2021
%if true
ax = gca;
ax.CLim = [abs(min(C{g,1}(:,1))) abs(max(C{g,1}(:,1)))];
Use abs for positive values

  0 Comments

Sign in to comment.


Image Analyst
Image Analyst on 21 Jan 2021
What are the values when it crashes:
minValue = min(C{g,1}(:,1))
maxValue = max(C{g,1}(:,1))
fprintf('Min = %f, max = %f.\n', minValue, maxValue);
% Apply min and max of our data. Min value at color #1, and max value will take on the color at the top of the colorbar.
colorbar;
caxis([minValue, maxValue]);
I'm guessing that maybe they're both the same value.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by