Creating a plot with a non-linear X-scale, Matlab - matlab

I'm trying to create a plot using the values 10^0 10^3 10^6 10^9... on the x-axis. Matlab does not automatically rescale so my values are all down in the left corner.
My current code is:
figure('name','My plot title');
hold on
plot(kap, reg, '--mo');
plot(kap, reij, '-.r*');
hold off
kap is a vector of x values -> 10^0 10^3 10^6 10^9.....,
reg and reij are measurements.
Maybe loglog, semilogx could help?
A picture of my plot: enter link description here
The problem was that I should use loglog or semilogx and not use it in a figure.

Yep, semilogx is what you want:
semilogx(kap,reg,'--mo');
loglog gives logarithmic scales for both x and y axes, and semilogy plots with a lagarithmic scale on the y axis.
x=10.^(0:3:30);
y=rand(size(x));
semilogx(x,y)

Related

MATLAB - Plotting a smooth volume from 3D scatter plot data

So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.

MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).

Plotting a 3D Function

Im trying to plot the following function:
F(x,y)=Cos(x)*Sin(y)
With the following features: Labeled all three axes, the x and y axes range from 0 to 2pi with 0.1 spacing, and the viewing angles are orthogonal to any x/y/z=0 plane.
I have the following code
t= 0:0.1:2*pi;
A=cos(t);
B=sin(t);
for i=1:numel(t)
C(:,i)=A(i).*B(:);
end
surf(C);
xlabel('x axis','fontsize',12)
ylabel('y axis','fontsize',12)
zlabel('z axis','fontsize',12)
title ('3D Plot')
That is almost what I need, for some reason the chart is plotting x and y from 0 to 80, and Im not sure why. I there another way I can try plotting this so that I can use different variables for the cosine and sine inputs?:
I think you wanna do
surf(t,t,C)
instead of only surf(C). Now it will range from 0 to 2pi. You need to define the x- and y-values if you want them to have specific values.
You might want to use this as well, to remove the unnecessary parts of the axises.
axis tight

How to make a semilog plot within a semilog plot in MATLAB?

Is it possible to create a semilog plot (semilogx, semilogy, loglog) within a semilog plot? I need to have a zoom-in plot. Most solutions I found only solve for linear scale but not log scale.
Try using axes, for example:
x = linspace(0,1);
figure(1)
% plot on large axes
semilogy(x,1./x)
% create smaller axes in top right, and plot on it
axes('Position',[.55 .55 .33 .33])
box on
loglog(x,exp(x))

Plotting points while plotting vectors : Matlab

I need to make a plot with only points and tried something like
plot(x,y)
where x and y are vectors: collection of points.
I do not want matlab to connect these points itself. I want to plot as if plotted with
for loop
plot;hold on;
end
I tried
plot(x,y,'.');
But this gave me too thick points.
I do not want to use forloop because it is time expensive. It takes a lot of time.
You're almost there, just change the MarkerSize property:
plot(x,y,'.','MarkerSize',1)
Try:
plot(x,y,'*');
or
plot(x,y,'+');
You can take a look to the documentation: http://www.mathworks.nl/help/matlab/creating_plots/using-high-level-plotting-functions.html
help scatter
IIRC: where S is the size of the scatter points:
scatter(x,y,S)
You may try this piece of code that avoid using loops. The plot created does not have lines but markers of different colors corresponding to each column of matrices x and y.
%some data (matrix)
x = repmat((2:10)',1,6);
y = bsxfun(#times, x, 1:6);
set(0,'DefaultAxesColorOrder', jet(6)); %set the default matlab color
figure('Color','w');
plot(x,y,'p'); %single call to plot
axis([1 11 0 70]);
box off;
legend(('a':'f')');
This gives