X axis scaling with matlab plotting - matlab

My data is sparse therefore when I plot my graph I get the following result
As you can see the first x axis tick starts at 500(s), but most of my data is around 30(s). Can I change the scaling of the x axis?

How about this?
X = [1 3 6 10 25 30 235 678 1248];
Y = [0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.8 0.9];
plot(X,Y,'-b.')
figure
semilogx(X,Y,'-b.')
I see the following output:

If you want to display data from 0 to 30s only you can either plot only those like this:
idcs=Xdata <30; %# find indices where X is less than 30s
plot(Xdata(idcs),Ydata(idcs),'b'); %#plot only these data.
or you can just express XLimits on the figure.
plot(Xdata,Ydata,'b'); %# plot everything
set(gca,XLim,[0 30]); %# limit display on X axis

Related

MATLAB: How to add custom ticks and labels to an imagesc plot?

I have made a matrix containing 13 different vectors with ~300K+ rows. I have visualized the matrix by transposing it and using the imagesc function to see the distribution of colors. All vectors have been resampled, processed and normalized between 0 & 1 individually.
The imagesc plot gives me this result (fig 1):
But, when I use the axis functionality to add x & y limits, I get this:
How do I maintain the imagesc plot while being able to add custom ticks and labels to the X & y axis? The x axis represents time, while the y axis will get its own labels with sensor names.
You redefine limits from 0 to 30 on the x-axis while the initial xlimits goes up to 3e5. Same issue with the y-axis
Here's how to redefine the Y-axis to put sensor names:
C = [0 2 4 6 9 ; 8 10 12 44 14; 16 48 10 32 23];
image(C)
% Get axis handle
ax = gca;
% Set where ticks will be
ax.YTick = [1 2 3];
% Set TickLabels;
ax.YTickLabel = {'S1','S2','S3'};
Figure out the ax.YTick where you want the labels to appear.
If you want the x-axis to go from 0 to 30, divide the x component of all vectors by 1e4 before plotting. Alternatively, you can add the line:
ax.XTickLabel = ax.XTick/1e4;

making a trendline in log-log scale scatter graph

I am coding to make a trendline on my scatter plot.
data=[];
for k=1:100
int=0;
for t=1:100
if k_star_90(k,t)~=0
int=int+k_star_90(k,t);
end
if k_star_90(k,t)==0 && int~=0
data=[data int];
int=0;
end
end
end
intervals = linspace(0, 1, 100);
h1 = histc(data, intervals);
scatter(intervals, h1, 'r');
set(gca,'xscale','log')
set(gca,'yscale','log')
picture of plot result
This is in log-log scale. On this plot, I want to draw y=ax+b(1st order) trendline. I am not sure how to do it.
I will really appreciate your help
I'm not sure I understood your intention correctly, but if you need a trend line you may do something like this
intervals = [0.01 0.02 0.2 0.1 0.3 0.5 0.03 0.4 0.15 0.2 0.2 0.25 1 0.9 0.8 0.8 0.7];
h1 = [70 40 4 20 2 3 60 10 50 40 10 20 1 2 3 1 2] ;
coeffs = polyfit(intervals, h1, 1);
xFitting = 0:0.01:1;
yFitted = polyval(coeffs, xFitting);
scatter(intervals, h1, 'r');
set(gca,'xscale','log');
set(gca,'yscale','log');
grid on;
hold on;
plot(xFitting, yFitted, 'b', 'LineWidth', 2);
hold off;
ylim([1 80]);
xlabel('intervals');
ylabel('h1');
Here is your trend in Log scale:
Og course it does not look like a first order trend. To depict it as a line you need to go back to a normal plot:

2D interpolation with interp2

I want to describe surface flatness of a plane with interp2 function.
Spatial sampling points are as below.
width=[0 500];
length=[0 100 200 300 400 500 600 700 800 900 1000];
and flatness are as below, at width 0 and 500, respectively.
a = [1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1]; % flatness at width 0
b = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]; % flatness at width 500
With these values, the surface shape will be like following figure.
I wanted to change this figure into following figure with interp2 function.
Below is my code.
widthq=[0 100 200 300 400 500];
flatness=[a' b'];
flatnessq=interp2(width,length,flatness,widthq,length);
But, not working with one error, 'The input data has inconsistent size.'
Can anyone explain this error and give a way how to interpolate my data with interp2 function?
The first three inputs need all to be of the same size
[W, L] = meshgrid(width, length);
The last two arguments need also to be of same size
[Wq, Lq] = meshgrid(widthq, length);
Then it should work
flatnessq=interp2(W,L,flatness,Wq,Lq);

Plot bar chart with specific range of x axis in MATLAB

I would like to plot a bar chart like below in MATLAB. Any one know which function should I use? Many thanks in advance!
The bar is specified with range in x (some may be wider than others).
There is no line between two bars (red cross in the figure).
X axis is in log scale.
why don't you try to do it with the plot or semilogx function?
x = [0.1 0.18 0.18 0.32 0.32 0.56 0.56 1.0];
y = [30 30 25 25 110 110 80 80];
semilogx(x,y);
and if you want the x ticks like in your figure you can set them on the axis object:
ax = gca;
ax.XTick = unique(x);
ax.XTickLabel = unique(x);

How to plot two stem plots and two box plots in one figure in MATLAB?

I have two vectors
A=[0.1 0.1 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.5 0.5]
B=[0.7 0.7 0.8 0.8 0.9 0.9 0.9 0.9 1 1 1]
How to plot their stem plots and box plots at the same time?
The y-axis should be the probability of the stem plots.
What I am looking for is something like this.
Drawing the box-plots can be accomplished with
boxplot([A B], [ones(size(A)) 2*ones(size(B))], ...
'orientation', 'horizontal', 'positions', [1 1]);
After which you can add the stem plots with
hold on
stem(xa, ya);
stem(xb, yb);
where I'm not sure exactly what you are asking for for x and y.