Contour colors don't correspond to color bar when a surf plot is added - matlab

Below is some code that recreates my problem as simplified as I can make it. It does a subplot with two plots, you'll notice the plot on the right (contour only) has the correct correlation between the contour colors and the color bar but when a surface is added (left plot) the colors no longer match up.
Notes:
I've tried contourslice but I get the same results. I've posted the code for that below too.
How far off the colors are seems to depend on the values of the contour data itself. If you replace my contour data with peaks, it works fine. However this does not solve the underlying problem.
Code using contour:
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
%make contour data
[x,y] = meshgrid(-1:.5:1,-1:.5:1);
con = (x.^2+y.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contour(x,y,con) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,con)
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Code using contourslice instead of contour (same problem)
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
x = -1:.5:1;
y = x;
z = x;
%make contour data
[xg,yg,zg] = ndgrid(x,y,z);
V = 3-(xg.^2+yg.^2+zg.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contourslice(x,y,z,V,[],[],0) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,V(:,:,3))
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Thanks for your help!

Just set the caxis property as you wish:
colorbar
caxis([0 2])
...
colorbar
caxis([0 2])
The problem was probably caused, because the surf plot changed the color determining values of your plot. By setting a fixed color axis you can avoid all misinterpretations.

Related

How to change size of grid & granularity in meshc function

Show mask by meshc function
z = [0 0 0 0;0 1 1 0;0 0 0 0];
meshc(z)
Output is:
Desired output:
There is a lot of guessing on my side, and I guess you want something like this:
%// data
z = [0 0 0 0;0 1 1 0;0 0 0 0];
%// grid
[n,m] = size(z);
[x,y] = ndgrid(1:n,1:m);
%// finer grid
[xq, yq] = ndgrid(linspace(1,n,100),linspace(1,m,100));
%// interpolation
F = griddedInterpolant(x, y, z, 'cubic')
zq = F(xq, yq);
%// interpolated plot
figure(1)
meshc(xq,yq,zq)

Matching Colormap and Colorbar

I'm running the following code
% x_start y_start x_end y_end concentration
A = [0 0 1 1 10
0 1 3 3 0.6
3 1 6 2 1.2];
cmap = jet(256);
con_min = 0;
con_max = 10;
ind_c = round((size(cmap,1)-1)*A(:,5)/(con_max-con_min))+1
figure;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');
plot([A(:,1) A(:,3)]',[A(:,2) A(:,4)]');
colorbar
However, my colorbar is not matching up with my colormap; it's using the default colorbar, and I'm running a custom colormap. How can I make it so that my colorbar shows the right values for my colormap?

Scatter plot in Matlab with x-axis ticks not equidistant

Consider the following scatter plot in Matlab
A=[1 0 0 0 2 0 0 0 0 0 0 0 1 2 0 0 0 2 1 200 300]';
xRange = 0: max(A);
prob=zeros(size(xRange,2),1);
for r=1:size(xRange,2)
prob(r) = sum(ismember(A,xRange(r)))/size(A,1);
end
scatter(xRange,prob, 'b');
xlim([-2 max(A)+2])
ylim([-0.5 1.5])
I want to change the way the scatter looks like in order to make it more clear: the idea is to put the following ticks on the x-axis
[-0.5 0 1 2 3 301]
but the tricky part is that they should be equidistant so that I can zoom on the part of the scatter plot with higher values of prob.
Any idea?
One way to achieve this is to transform the data to a new scale using interpolation. Let's say you want the data values
tickVal = [-0.5 0 1 2 3 301];
to appear at the graphical positions
tickPos = 0 : 5;
Determine the graphical positions for all the data by interpolation, and plot the transformed data:
xTransformed = interp1(tickVal, tickPos, xRange);
scatter(xTransformed, prob, 'b');
xlim([min(tickPos), max(tickPos)])
ylim([-0.5 1.5])
Now we have to make sure that the ticks do not reflect the transformed, but the original data values:
set(gca, 'XTick', tickPos)
set(gca, 'XTickLabel', num2cell(tickVal))
The result looks like this:
Is this what you want?

Modifying subplot axes in matlab

I'm plotting 2 subplots, and I want each subplot to have different axis scaling.
My current code is:
subplot(2,1,1)
axis([0 20 0 1])
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
subplot(2,1,2)
axis([0 5 0 1]);
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
But both subplots plot as if no axis was specified.
Any ideas?
Try:
subplot(2,1,1)
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
axis([0 20 0 1])
subplot(2,1,2)
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
axis([0 5 0 1]);
plot will refigure the axes, so you have to call axis AFTER the last plot.

How can I display a 2D binary matrix as a black & white plot?

I have a 2D binary matrix that I want to display as a black and white plot. For example, let's say I have a 4-by-4 matrix as follows:
1 1 0 1
0 0 1 0
1 1 0 1
1 0 0 0
How can this be plotted as a black and white matrix? Some of my input binary matrices are of size 100-by-9, so I would ideally need a solution that generalizes to different sized matrices.
If you want to make a crossword-type plot as shown here (with grid lines and black and white squares) you can use the imagesc function, a gray colormap, and modify the axes properties like so:
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix
[r, c] = size(mat); % Get the matrix size
imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image
colormap(gray); % Use a gray colormap
axis equal % Make axes grid sizes equal
set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties
'XLim', [1 c+1], 'YLim', [1 r+1], ...
'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on');
And here's the image you should get:
I'm not sure if I got your question right, but you may try the image function, like this:
A = [ 1 1 0; 1 0 1; 1 1 1 ];
colormap([0 0 0; 1 1 1 ]);
image(A .* 255);
Try the spy function to start with perhaps.