Matlab bar plot in specific time points of each measured values - matlab

I want to plot vertical lines corresponding to values obtained at specific time points.
Example:
a = [0 5 7 9 ] at 0 seconds,
b = [0.5 6 6.5 11] at 2 seconds,
c = [0 4 2 10] at 4 seconds
Each time point will be a vertical line between the maximum and minimum of the vectors. I also need to mark the start and end points of a, b and c, for instance a should have a circle (or star etc.) at 0 and 9.
Here is an example output:

You can use line with end markers.
% Your data
a = [0 5 7 9 ];
b = [0.5 6 6.5 11];
c = [0 4 2 10];
% Combine to get min/max values
data = [a; b; c].';
mins = min(data);
maxs = max(data);
% Plot using line, nice flexible method which plots vertical lines at points 2:2:n
line(repmat(2*(0:numel(mins)-1), 2, 1), [mins; maxs], 'color', 'k', 'marker', 'o')
Output:
If you want different markers on each end, or different colours, please see this answer which gives more detailed examples.

Related

Plot data ignoring some X-axis values on Matlab

I'm trying to plot some data on Matlab using the following code:
x = [1 2 5 6 7 9]
y1 = [1 2 3 2 1 2]
y2 = [2 2 2 1 3 3]
y3 = [1 1 2 3 1 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
And I'm getting the following result:
Note that I have no Y values for the X positions 3, 4 and 8, but the X axis is still showing these X values in the graph.
There is some way that I can ignore the positions 3, 4 and 8 in the X-axis, and show only the Y values for the X positions 1, 2, 5, 6, 7 and 9?
I can use the following command to 'hide' these positions:
set(gca, 'XTick', x);
But the gaps related to these positions are still there.
Update:
This is the graph I'm trying to create (it was created on paint):
Note: in my case, the X-axis just represents the IDs of some images, and because of that I just need to show the numbers.
You can get the plot you want by first leaving x out in the calls to plot (so it plots against the array index) then altering the XTick and XTickLabel properties of the axes (and adjusting the x limit slightly):
plot(y1,'--.','markersize',20); hold on;
plot(y2,'--.','markersize',20);
plot(y3,'--.','markersize',20);
legend('y1','y2','y3');
xlim([1 numel(x)]); % Note numel is used here
ylim([0 4]);
set(gca, 'XTick', 1:numel(x), 'XTickLabel', cellstr(num2str(x(:))));
I am not sure if you need gaps between plot lines. If you wish no values to be plotted there, try replacing the values by nan. For example,
x = [1 2 3 4 5 6 7 8 9]
y1 = [1 2 nan nan 3 2 1 nan 2]
y2 = [2 2 nan nan 2 1 3 nan 3]
y3 = [1 1 nan nan 2 3 1 nan 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
This will give a plot like this:
Sounds like you need stem.
stem(x, y1);
legend('y1');

How to draw errorbar on one series out of several of a bar graph

I am not familiar with errorbar function in Matlab. The bar graph I am drawing has two series. I just need to plot the error bar for one of them. How should I plot this? Thank you! The error data for series 1 is defined in error_1 variable.
series_1 = [ 10 20 30 40 50];
series_2 = [2 4 6 8 10];
error_1 = [0.5 1.0 1.5 2.0 2.5];
a = [series_1' zeros(length(series_1),1)];
b = [zeros(length(series_1),1) series_2'];
figure(1);
[AX,H1,H2] = plotyy(1:length(series_1),a,1:length(series_2),b,'bar','bar');

Matlab - Plot multiple lines using multiple points and find the length of each line

I have two matrix as
A = [1 2 3; 4 6 7; 3 6 7]
B = [2 5 6; 2 8 7; 2 8 5]
I want to plot a graph between this two matrix, I mean in such as way that A(1,1) as x coordinate and B(1,1) as Y coordinate of 1st point. Similarly, for 2nd point A(1,2) as x and B(1,2) as Y and so on. At last I should get straight line connecting this point for each row.
And then I have the measure the length of the line connecting all the points for each row, so that I can know which row have greater length
I tried this
for i=1:1:3
plot(A(i,:),B(i,:)), hold on;
end
Is it correct because I can't able to interpret and how to measure the length also??
Your way of plotting seems correct.
To calculate the length of each line I would use this code:
for i=1:1:3
len(i) = sum(sqrt(diff(A(i,:),1).^2+diff(B(i,:)).^2));
end
You don't need for loop to plot. Just do.
A = [1 2 3; 4 6 7; 3 6 7];
B= [2 5 6; 2 8 7; 2 8 5];
% Plot lines
plot(A.',B.');
% Calculate length of lines
length=sum(sqrt((diff(A,1,2).^2)+(diff(B,1,2).^2)),2);

How to label plot having peaks in matlab

I'm trying to label my XRD data which have peaks, and I want to label it from my array of data:
peak label
ab
ac
ad
cb
bb
ba
See picture below
I also want those labels to be vertically aligned on the top of the peaks.
I tried the findpeaks function but it doesn't work.
Try this (but you need to have a Signal Processing Toobox):
x = [1 2 3 4 5 6 7 8 9]
y = [1 4 2 7 3 9 5 10 2]
[peak, peakId] = findpeaks(y); %find peaks in your serie
figure(1)
plot(x, y)
lbalph=('a':'z').'
lb=strcat(Alphabet(1),lbalph(1:length(peak))) %Create a label matrix
lb = num2cell(lb,2) % Convert to cell array
lbid = 1:length(lb)
text(x(peakId), peak, lb(lbid),'Rotation',90) % label the peak with your lb matrix
As you have the index peaks, you can labeled as you want.

Color Time Series Based on The Cluster Assignment

Say I have the following Time Series:
a = [1 3 1 5 1 3 5 1 5];
Now, on doing kmeans(a,3), I obtained:
b = kmeans(a,3); // [1 2 1 3 1 2 3 1 3]; based on the clusters.
I now wish to plot a, so that the color for a(i) corresponds to the cluster that is has been assigned in b. Can someone show me how to do that?
This plots each point with increasing x coordinates, y coordinate equal to a, and the color as given by b:
colors = hsv(max(b)); %// or use other color maps:
hold on
for ii = 1:length(a)
plot(ii,a(ii),'marker', 'o', 'color',colors(b(ii),:)) %// option 1
end
axis([0 length(a+1) min(a)-1 max(a)+1])