Modifying subplot axes in matlab - 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.

Related

Rotation increments are off on 3d object

I can rotate an object about an axis but theta is very off
it should be rotating from 1 degree to 45 degrees in 1 degree increments but it's doing much more than that. I converted theta (th) from degrees to radians but the issue still occurs.
Here's a link to the animation of the rotation
https://www.dropbox.com/s/bnc7m4fxipicizr/rot2.gif
And the code is below:
clf
[Z,Y,X] = cylinder(10:-1:0, 50);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
array_sz_begin=size(X);
W=repmat(1,array_sz_begin); %create ones for w
figure(1), clf;surf(X,Y,Z);axis equal;
%--- z-rotation matrix Rz
for n=1:1:45
th=n*pi/180; %angle of rotation converted to radians;
Rz=[cos(th) -sin(th) 0 0;sin(th) cos(th) 0 0;0 0 1 0;0 0 0 1];
P=[X(:) Y(:) Z(:) W(:)]*Rz; %rotate each point on surface
X=reshape(P(:,1),array_sz_begin);%transform surface vertices back
Y=reshape(P(:,2),array_sz_begin);
Z=reshape(P(:,3),array_sz_begin);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
clear P;
title(['Shift in ',num2str(n),' deg']);
hold on;surf(X,Y,Z);axis equal;
pause (.5)
end
PS I'm using octave 4.0 which is like matlab.
Decided to post the solution in case someone has an issue like this. All thanks goes to BillBokeey
clf
[Z_orig,Y_orig,X_orig] = cylinder(10:-1:0, 50);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
array_sz_begin=size(X_orig);
W=repmat(1,array_sz_begin); %create ones for w
figure(1), clf;surf(X_orig,Y_orig,Z_orig);axis equal;
%--- define z-rotation matrix Rz
for n=1:1:90
th=n*pi/180; %angle of rotation converted to radians;
Rz=[cos(th) -sin(th) 0 0;sin(th) cos(th) 0 0;0 0 1 0;0 0 0 1];
%Rz=[cos(th) -sin(th) 0;sin(th) cos(th) 0;0 0 1];
P=[X_orig(:) Y_orig(:) Z_orig(:) W(:)]*Rz; %rotate each original point on surface
X=reshape(P(:,1),array_sz_begin);%transform surface vertices's back
Y=reshape(P(:,2),array_sz_begin);
Z=reshape(P(:,3),array_sz_begin);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
clear P;
title(['Rotate in ',num2str(n),' deg']);
hold on;surf(X,Y,Z);axis equal;
pause (.1)
clear P
end

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?

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

how to change axis sizes in Matlab

I have a plot like this
How can I change the length of y axis to [0 20] in order to have bigger plot size.
Assume I want x axis [0 20]
y axis [0 20]
Immediately after the plot command:
set(gca, 'ylim', [0 20], 'xlim', [0 20]);