Plot data ignoring some X-axis values on Matlab - 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');

Related

How to shade area and make it transparent between two lines in MATLAB?

I shaded the area between two lines, it's not very clean:
area(xData,[Y1(:) ,Y2(:)-Y1(:)]); hold on
colormap([1 1 1; 0 0 1]);
How to make it transparent too in MATLAB? So that it comes like:
ref:peltiertech.com
You can use the FaceAlpha property of the area object to set the transparency level:
xData = 1:7;
Y1 = [3 2 1 4 3 2 1];
Y2 = [8 6 9 8 7 5 6];
area(xData, Y2, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3);
hold on
area(xData, Y1, 'EdgeColor',[0 .447 .741], 'FaceColor', [1 1 1]);
A cleaner approach is to use patch instead of area:
h = patch([xData xData(end:-1:1) xData(1)], [Y1 Y2(end:-1:1) Y1(1)], 'b');
set(h, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3)

Matlab bar plot in specific time points of each measured values

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.

plot vectors of data

I have three vectors of data; first column is axis x, second column axis y and third column is the measured data point value v:
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
Is there a way to plot this in matlab and color the data points according to their value? I tried with scatter, but that doesn't give color coding.
Just use scatter, it can accept values for each point. Just set the size of each point, set it to be filled, turn on the colorbar and that's it. Just please don't use the jet colormap...
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
pt_sz = 30;
colormap parula
scatter(x, y, pt_sz, v, 'filled');
colorbar;
grid on
You can try pcolor. It works like
z=rand(4,4);
pcolor(R,C,z);
Where z is a matrix storing the data (your v), R is the row (your x) and C is the column (your y).

Add markers to a step function in Matlab

I have the following step function:
I wish to add 2 markers, on the left hand side of the horizontal line as a filled circle and on the right hand side a circle which is not filled with color. How can I do this please? Thank you.
I used very simple code for this graph. The code is:
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
x=[1.5 2.6];
y=[2 2];
plot(x, y)
hold on
x=[2.6 5];
y=[3 3];
plot(x, y)
hold on
x=[5 6];
y=[4 4];
plot(x, y)
hold on
x=[6 6.5];
y=[5 5];
plot(x, y)
hold on
x=[6.5 8];
y=[6 6];
plot(x, y)
axis([0 9 0 7])
Do as follows for each segment (the new lines are the last two):
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
plot(x(1),y(1),'o','MarkerFaceColor','b') %// filled circle
plot(x(2),y(2),'o') %// non-filled circle

matlab x axis label set as a vector

How can I set the x axis label as a vector? For example, if I do plot(1:5), the x axis label is [1, 2, 3, 4, 5]. I'd like to set it to a vector, e.g. [1 4 5 7 10]. Note that the vector's size may be huge, so doing it manually is not acceptable.
I believe this is what you want.
y = 1:5;
x = [1 4 5 7 10];
plot(y);
set(gca,'XTickLabel',x);
you can do this by sending plot two vectors: one for x and one for y.
plot([1 4 5 7 10], 1:5);