Skip to content

Commit

Permalink
plots from fileexchange for showing A matrix values
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccamccabe committed Apr 17, 2024
1 parent a7d0620 commit 176712e
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
122 changes: 122 additions & 0 deletions mdocean/plots/bluewhitered.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
function newmap = bluewhitered(m)
%BLUEWHITERED Blue, white, and red color map.
% BLUEWHITERED(M) returns an M-by-3 matrix containing a blue to white
% to red colormap, with white corresponding to the CAXIS value closest
% to zero. This colormap is most useful for images and surface plots
% with positive and negative values. BLUEWHITERED, by itself, is the
% same length as the current colormap.
%
% Examples:
% ------------------------------
% figure
% imagesc(peaks(250));
% colormap(bluewhitered(256)), colorbar
%
% figure
% imagesc(peaks(250), [0 8])
% colormap(bluewhitered), colorbar
%
% figure
% imagesc(peaks(250), [-6 0])
% colormap(bluewhitered), colorbar
%
% figure
% surf(peaks)
% colormap(bluewhitered)
% axis tight
%
% See also HSV, HOT, COOL, BONE, COPPER, PINK, FLAG,
% COLORMAP, RGBPLOT.


if nargin < 1
m = size(get(gcf,'colormap'),1);
end


bottom = [0 0 0.5];
botmiddle = [0 0.5 1];
middle = [1 1 1];
topmiddle = [1 0 0];
top = [0.5 0 0];

% Find middle
lims = get(gca, 'CLim');

% Find ratio of negative to positive
if (lims(1) < 0) & (lims(2) > 0)
% It has both negative and positive
% Find ratio of negative to positive
ratio = abs(lims(1)) / (abs(lims(1)) + lims(2));
neglen = round(m*ratio);
poslen = m - neglen;

% Just negative
new = [bottom; botmiddle; middle];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, neglen);
newmap1 = zeros(neglen, 3);

for i=1:3
% Interpolate over RGB spaces of colormap
newmap1(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end

% Just positive
new = [middle; topmiddle; top];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, poslen);
newmap = zeros(poslen, 3);

for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end

% And put 'em together
newmap = [newmap1; newmap];

elseif lims(1) >= 0
% Just positive
new = [middle; topmiddle; top];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, m);
newmap = zeros(m, 3);

for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end

else
% Just negative
new = [bottom; botmiddle; middle];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, m);
newmap = zeros(m, 3);

for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end

end
%
% m = 64;
% new = [bottom; botmiddle; middle; topmiddle; top];
% % x = 1:m;
%
% oldsteps = linspace(0, 1, 5);
% newsteps = linspace(0, 1, m);
% newmap = zeros(m, 3);
%
% for i=1:3
% % Interpolate over RGB spaces of colormap
% newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
% end
%
% % set(gcf, 'colormap', newmap), colorbar
18 changes: 18 additions & 0 deletions mdocean/plots/imrange.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function [minval maxval] = imrange(inpict)
% [min max] = IMRANGE(INPICT)
% returns the global min and max pixel values in INPICT
%
% INPICT can be a vector or array of any dimension or class

x = inpict(:);
minval = double(min(x));
maxval = double(max(x));

if nargout < 2
minval = cat(2,minval,maxval);
end

end



24 changes: 24 additions & 0 deletions mdocean/plots/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2009, Nathan Childress
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
48 changes: 48 additions & 0 deletions mdocean/plots/signed_log.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function signed_log(A)
% https://www.mathworks.com/matlabcentral/answers/1700655-symmetric-diverging-log-color-scale#answer_1380451
figure
% plot A
% i'm going to plot xdata just for sake of giving some scale information
imagesc(signedlog10(A)) % represented in signed log10
% use a symmetric colormap
colormap(bluewhitered())
% get the minimum and maximum value of A
% this needs to be symmetric unless you also make a
% custom asymmetric divergent colormap
% the bluewhitered colormap is smart and adjusts to all pos/neg so need
% to account for this in crange.

arange = max(abs(imrange(A)));
if all(A>=0,'all')
crange = [0 arange];
crange = [0 1] + fix(signedlog10(crange));
elseif all(A<=0,'all')
crange = [-arange 0];
crange = [-1 0] + fix(signedlog10(crange));
else
crange = [-1 1]*arange; % make symmetric
crange = [-1 1] + fix(signedlog10(crange));
end
% you may choose to round to integer values
% set limits for the caxis
caxis(crange); % represented in signed log10
% create ticks, ticklabels
% choose nticks as desired
nticks = max(crange)+1;
ticklabels = logspace(0,max(crange),nticks);
ticklabels = [-flip(ticklabels) 0 ticklabels]; % make symmetric
% set Ticks and TickLabels
colorbar('Ticks',signedlog10(ticklabels),'TickLabels',ticklabels)
improvePlot
axis square
end

function out = signedlog10(in)
% naive signed-log
%out = sign(in).*log10(abs(in));

% modified continuous signed-log
% see Measurement Science and Technology (Webber, 2012)
C = 0; % controls smallest order of magnitude near zero
out = sign(in).*(log10(1+abs(in)/(10^C)));
end

0 comments on commit 176712e

Please sign in to comment.