0

I want to plot all lines parallel to the y axis with x=10, 20,30,...,100

I wrote x as:

x=linspace(1,100,10);

and I try to plot in this way:

figure(1)
plot([x; x], [zeros(1, length(x))*min(ylim); ones(1, length(x))*max(ylim)],'r')

but this does not work well. How can I write y to plot such lines?

user3582433
  • 469
  • 1
  • 8
  • 24
  • You say x=10, 20,,...,100 but the `linspace(1,100,10);` create a vector with start value at 1. You should use `linspace(10,100,10);` or better simply `10:10:100` – NLindros Jan 25 '17 at 09:59

2 Answers2

1

You have the right idea, but both your x-coordinates and your y-coordinates for the line ends are wrong. For x, you should use:

x = 10:10:100;

This generates [10, 20, ..., 100]. linspace(1, 100, 10) on the other hand generates 10 equally spaced values between 1 and 100 - which is somewhat different. To get the same values using linspace you would do linspace(10, 100, 10).

For y, because you use zeros, the line only extends from zero to the upper y limit, rather than from the lower to the upper limit. Your call should instead be:

plot([x; x], repmat(ylim', 1, numel(x)), 'r')

This repeats the y-axis limits for each line, so the i-th line is drawn from (x(i), ylim(1)) to (x(i), ylim(2)).

buzjwa
  • 2,632
  • 2
  • 24
  • 37
  • in this way the lines parallel to the y axis, have not x = 10,20,30, ... 100 but have an x a little bigger about x=11,25... – user3582433 Jan 25 '17 at 09:54
  • That's because of how you use `linspace`. There's an error there too, I will edit the answer. – buzjwa Jan 25 '17 at 09:55
1

This doesn't exactly answer your question, but might be what your looking for.

Use XGridproperty to create (support) lines parallel to the Y-axis.

ax = axes;
ax.XGrid = 'on';

or for older Matlab versions

set(ax, 'XGrid', 'on')

(If you don't have the axes handle ax, you could use gca)

This create lines at the current XTick of the plot. If you want to have the grid lines a specific range you have to change the XTick

ax.XTick = 0:10:100;

If you think that the lines are to weak (hard to see), you can turn up the grid alha value (default value is 0.5).

ax.GridAlpha = 1; %

or color it

ax.GridColor  = 'r'; % Set grid color to red
NLindros
  • 1,683
  • 15
  • 15