3D Surface plot misplaced axis - matlab

The code is:
subplot(1,3,3)
h=surf(ReflMatrix)
set(h, 'edgecolor','none')
colormap winter %Other colourmaps: Winter,Cool
hold on;
ylabel('frequency (Hz)');
xlabel('angle of incidence (degrees)');
alpha(.5) %transparency
The ReflMatrix is 401x90. The values of y range from 0 to 90, which is good because y is angle measured in degrees . The values of x (frequency) range from 0 to 401 because my bandwidth is 401 frequencies but I would like the same graph with values ranging from 300 to 700 (instead of starting from frequency 0 to start from frequency 300).

In surf you can specify your x and y. In your case, define your frequency by
y = linspace(300,700,401);
and the phase by
x = linspace(0,90,91);
Are you sure with the size of ReflMatrix, since frequencies from 0 to 90 are 91 points rather than 90. Then set your x and y parameters according to
[X,Y] = meshgrid(x,y);
h = surf(X,Y,ReflMatrix);
EDIT:
You can set the limits of the axes accordingly by
xlim([0 90]);
ylim([300 700]);
zlim([min(min(ReflMatrix)) max(max(ReflMatrix))]);

Related

How to set theta limit in -90 to 90 range in MATLAB?

How to set theta range from 0 to 90 and 270 to 360 in a single plot.
thetalim([theta_lower,theta_upper])
To set it from -90° to 90°, just set it from -90° to 90° i.e.
%Creating a random polar plot with same ThetaDir and ThetaZeroLocation as yours
theta = linspace(0, 2*pi);
rho = rand(1, 100);
polarplot(theta, rho);
ax = gca;
set(ax,'ThetaDir', 'clockwise', 'ThetaZeroLocation', 'top');
%Setting the desired limits
thetalim([-90 90]);
and if you want to have positive values for theta then you can change the ticklabels as follows:
ax.ThetaTickLabel = wrapTo360(ax.ThetaTick); %requires Mapping Toolbox
% or without Mapping Toolbox:
% ax.ThetaTickLabel(ax.ThetaTick<0) = split(num2str(ax.ThetaTick(ax.ThetaTick<0) + 360));

How to turn y axis of histogram to show percentage ranging from 0 to 1

I want to change the y axis of the histogram to show percentages ranging from 0 to 1. this is what I've tried, but it doesn't seem to be working.
myTolerance=1e-12; % in erg units.
nbins=50;
for j=1:ntM/100:ntM
H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);
%Select from column j all rows in column j whose absolute values are
%greater than the tolerance.
H(1).delete; %%Remove bins, only keep the fit.
set(gca, 'YScale', 'log');
set(gca, 'XScale', 'log'); % Make logarithmic X
yt = get(gca, 'YTick');
set(gca, 'YTick', yt, 'YTickLabel',
yt/numel(Wkinet(abs(Wkinet(:,j))>myTolerance)))
pause;
end
This is what is currently looks like:
This is what I want:
Just to simplify the discussion below, the line
H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);
is equivalent to
data = Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV;
H = histfit(data, nbins);
This means below we'll assume data is a vector.
histfit computes and plots a histogram though histogram, then fits a function to it through fitdist. Since you don't want to plot the histogram itself, just stick to fitdist:
pd = fitdist(data,'Normal'); % this is the default distribution used in `histfit`, is it correct?
x = linspace(min(data),max(data),200); % 200 points in the graph, you might want to change this?
y = pdf(pd,x);
plot(x,y);
Now it's simple to normalize the plot however we want. For example set the first element to 1:
pd = fitdist(data,'Normal');
x = linspace(min(data),max(data),200);
y = pdf(pd,x);
y = y/y(1); % <<< Normalize
plot(x,y);
You can set limits on your y-axis using
ylim([1e-3 1]) %lower limit is nonzero since it's plotted on log scale
or
set(gca, 'ylim', [1e-3 1])

Saving two fig that can be superposed_ Matlab

I am saving two matlab figures as png and I want them to have the same size in order to be perfectly superposable.
The first figure is computed by the function 'FilledCircle2' which is a circle divided in half with two colors.
The second figure is computed by function 'FilledCircleL' which is the left half of the circle computed by the function 'FilledCircle2'.
I want to be able to have two figures, both with the same size so they can be perfectly superposed.
Can someone help me understand what I am doing wrong?
Here is my code, with both of the functions and respective outputs:
function []=FilledCircle2(x0,y0,Radius,N, col1, col2)
if(N<=1)
error('N must be greater than 1');
end
hold on
axis equal
axis off
hold on
t=(0:N)*2*pi/N; %t=-pi:0.01:pi
x=Radius*cos(t)+x0;
y=Radius*sin(t)+y0;
plot(x,y)
hold on
%Divide circle in to 2 equal parts
n=2;
thetas = linspace(-pi, pi,n+1); %linspace generates n points. The space between the points is [(pi/2)-(-pi/2)]/(n)
% Specify any colors wanted
colors = [col1; col2];
for k = 1:n
tt = linspace(thetas(k), thetas(k+1));
xi = Radius * cos(tt) + x0;
yi = Radius * sin(tt) + y0;
c2= fill([xi(:); x0], [yi(:); y0], colors(k,:)); %Assign diffrent colors to each circle 'slice'
set (c2, 'edgecolor','white')
set(c2,'LineWidth',2.0)
set(gcf,'PaperUnits','inches','PaperSize',[0.8666,0.8666],'PaperPosition',[0 0 0.8666 0.8666])%setting size (130/150, 130/150, 150pixels per inch being the default size of img), paper position is imporrtant as otherwise i will have extra border
set(gca, 'Position', [0 0 1 1]);
set(gcf,'color',[0.49019607843137 0.49019607843137 0.49019607843137])%figure properties, rgb(125/255,125/255,125/255)
fig = gcf;
fig.InvertHardcopy = 'off'; %saves the fig with the set background color
%rotates the plot
az= 90; %azimuth, az, is the horizontal rotation about the z axis as measured in degrees from the negative y-axis. Positive values indicate counterclockwise rotation
el= 90; % vertical elevation of the view point in degrees
view(az,el);
hold on
end
Here is the output of the function FilledCircle2(0,0,10,300, 'y', 'r'):
[
function []=HalfFilledCircleL(x0,y0,Radius,N, col1)
if(N<=1)
error('N must be greater than 1');
end
hold on
axis equal
% axis tight
axis off
hold on
t=(0:N)*(-pi)/N; %t=-pi:0.01:pi
x=Radius*cos(t)+x0;
y=Radius*sin(t)+y0;
hold on
c1=fill(x,y,col1); %filling the semi-circle
set (c1, 'edgecolor','white') %setting the outline color of the semi-circle
set(gcf,'color',[0.49019607843137 0.49019607843137 0.49019607843137])%figure properties, rgb(125/255,125/255,125/255)
set(c1,'LineWidth',2.0)
set(gcf,'PaperUnits','inches','PaperSize',[0.8666,0.8666],'PaperPosition',[0 0 0.8666,0.8666])%setting size (130/150, 130/150, 150pixels per inch being the default size of img), paper position is imporrtant as otherwise i will have extra border
set(gca, 'Position', [0 0 1 1]);
fig = gcf;
fig.InvertHardcopy = 'off'; %saves the fig with the set background color
% %rotates the plot
az= 90; %azimuth, az, is the horizontal rotation about the z axis as measured in degrees from the negative y-axis. Positive values indicate counterclockwise rotation
el= 90; % vertical elevation of the view point in degrees
view(az,el);
end
Here is the output of the function HalfFilledCircleL(0,0,10,300, 'r'):
use xlim and ylim to set the xy limits of the axes:
figure;
HalfFilledCircleL(0,0,10,300, 'r');
xlim([-12 12]);ylim([-12 12]);
az= -90; %azimuth, az, is the horizontal rotation about the z axis as measured in degrees from the negative y-axis. Positive values indicate counterclockwise rotation
el= 90; % vertical elevation of the view point in degrees
view(az,el);
figure;
FilledCircle2(0,0,10,300, 'y', 'r');
xlim([-12 12]);ylim([-12 12]);

consistent axis scale in matlab plot

I am using matlab for plotting scatter data. I want that the figure be in the range of [0 68] for X and [0 100] for Y, but when I use the following command, the X and Y axis are not consistent. For example, I expect the vertical axis to be longer than horizontal, while matlab give me something else. Have I missed something in the figure setting?
figure, axis([0 68 0 100]); box off , scatter(y,x,100,val,'filled'); box on;
It seems to be a matter of the order of commands.
x = 1:60;
y = 1/3.*x;
plot(x,y)
grid on
axis([0 60 0 20])
axis equal
will return
what you don't want, as it screws up your limits.
So rather use:
axis equal
axis([0 60 0 20])
and it is alright:

Hist3 Plotting and Axes Range

I'm plotting a 3-D histogram with MATLAB and it's working pretty ok, except for the different axes ranges. I'd like them to be defined in a way, where equal value pairs lie on the bisecting line.
My code looks like this (more or less "stolen" from the hist3 MATLAB example):
[vec_voxel_ids, vec_dose_values_reference, vec_dose_values_control] = ...
textread('_BOOSTINT_voxel_data.txt', '%u %u %u');
mat_dose_values = [vec_dose_values_reference, vec_dose_values_control];
hist3(mat_dose_values, [100, 100]);
xlabel('Dose Reference');
ylabel('Dose Control');
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
This is how it looks:
In order to reposition the bins to align their centers the ticks and also choose the range of the bin values you can use hist3's 'edges' option (similar to that in histc):
data = 500+5*randn(1e3,2); % dummy data
d = 1; % width for x and y bins
x = 480:d:520; % range for x bins
y = x; % use same range for y bins, can be different
hist3(data, 'edges', {x-d/2,y-d/2}); % subtract half bin width to center bins
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
view(2)
axis equal
axis([478 522 478 522])
grid minor
xlabel('x')
ylabel('y')
This example produces something like this:
Note, that this is a re-binning of your data so your output may look slightly different compared to before. The bins have been shifted to align their centers and the histogram recalculated.