MATLAB Answers

Plotting data with different colour based on sign only

1 view (last 30 days)
I am trying to plot a set of data using the code:
data=xlsread('test_stress.xls');
imagesc(data');
%colormap([0 0 1; 1 0 0]); %blue; red
ax = gca;
ax.YTickLabel = {'7','10','36','46','54', '62', '63', '71', '82', '84', '90'};
colormap(bluewhitered(256)), colorbar
Now I am getting, as expected, a plot based on colour gradient. However what I want is that irrespective of the values, just based on the sign there should be a plot, i.e. whenever it is positive, it will be red and whenever negative, it should be blue. And zero gives white. How do I go about it?

Accepted Answer

Guillaume
Guillaume on 3 Jun 2015
A simple way would be to change the CLim property of the axis. If you want white for 0, you also need to have it in the colormap:
imagesc(data');
colormap([0 0 1; 1 1 1; 1 0 0]);
ax = gca;
ax.CLim = [-eps; eps]; %Or hardcode some small enough constant

More Answers (1)

Walter Roberson
Walter Roberson on 3 Jun 2015
imagesc(sign(data'));
colormap([0 0 1; 1 1 1; 1 0 0]);
  1 Comment
Guillaume
Guillaume on 3 Jun 2015
Yes, actually it's simpler and I see no reason to plot the original data.

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