Scatter plot in Matlab with x-axis ticks not equidistant - matlab

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?

Related

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

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.

How to create a surface plot on a multiple line 3d plot?

Using plot3 I have created a multiple discrete line 3d plot. How can I fill the 3d surface these lines create (basically join the lines with a surface)?
Using fill3 can help.
x = [0 0 5 5 0];
y = [5 5 0 0 5];
z = [0 5 5 0 0];
subplot(1,2,1)
plot3(x,y,z,'LineWidth',5);
grid on
subplot(1,2,2)
fill3(x,y,z,'y')
grid on

Matlab: Using imagesc in grayscale without black

Beginning with a simple 2D matrix m of either 0 or 1 as an example:
m = [ 0 0 0 1 1
0 1 1 1 0
1 1 0 0 1
0 0 0 1 0 ]
How would I get this image to be displayed in a figure as only white and one gray using imagesc()? Currently, my code would is something like this:
imagesc(m)
colormap(gray)
colorbar
I've experimented with various ways to adjust the colormap and set limits on which values are used with CLim, but I have not found a way to limit the actual colors themselves so that instead of having white and black for values of 0 and 1, we have a white and (light) gray value for 1 and 0, respectively. Any quick and easy ways to do this?
Note: I should also mention that I want these so I can overlay a a contour plot of the same dimensions (but different values) and by using black, so I'm not sure if that would factor into any answers, but I am also open to suggestions with that in mind.
imagesc(m)
cmap = [.7 .7 .7 %// light gray
1 1 1] %// white
colormap(cmap)
colorbar('Ytick',[.25 .75],'Yticklabel',[0 1]) %// only two values in colorbar

display of data in meaningful color in Matlab scatter plots

I have about 9000 x,y locations, each with a value, say between -1 and 1. Let's call it a temperature.
I would like to plot each point with red/blue proportional to the point's temperature.
I can scale the values, in RGB, from [0 0 1] to [1 0 0] with G=0, and R+B=1. I want to the coldest points, with temperature -1, to be plotted with [0 0 1], pure blue, a mid-level point might be at [.4 0 .6], and a very hot point would be at [.95 0 .5]
I think there's a way to do this with the scatter command, but I can't figure out how to give a different RGB value to each point. If there's a way to do this with a plot command, I'd be happy to do that instead.
I'd appreciate any guidance you can give me.
Thank you,
Phil
Use the cdata property:
cdata = [0 0 0;
1 0 0;
0 1 0;
0 0 1;
1 0 1];
figure;
scatter(1:5, 1:5, 'o','cdata',cdata)

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.