1
votes

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:

enter image description here

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.

enter image description here

Update:

This is the graph I'm trying to create (it was created on paint):

enter image description here

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.

3

3 Answers

2
votes

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(:))));

enter image description here

1
votes

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: enter image description here

0
votes

Sounds like you need stem.

stem(x, y1);
legend('y1');

enter image description here