Matlab:绘制平行于 y 轴的线

Matlab: plot lines parallel to the y axis

我想绘制所有平行于 y 轴的线 x=10, 20,30,...,100

我把 x 写成:

x=linspace(1,100,10);

我试着这样画:

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

但这并不奏效。我怎样才能写 y 来绘制这样的线?

你的想法是对的,但是你的 x-coordinates 和你的行尾 y-coordinates 都是错误的。对于 x,您应该使用:

x = 10:10:100;

这会生成 [10, 20, ..., 100]。另一方面,linspace(1, 100, 10) 生成 10 个介于 1 和 100 之间的等距值 - 这有些不同。要使用 linspace 获得相同的值,您可以执行 linspace(10, 100, 10).

对于y,因为你使用了zeros,这条线只从零延伸到y的上限,而不是从下限延伸到上限。你的电话应该是:

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

这对每条线重复了 y-axis 限制,因此第 i 条线是从 (x(i), ylim(1))(x(i), ylim(2)) 绘制的。

这并不能完全回答您的问题,但可能正是您要找的。

使用XGrid属性创建(支撑)平行于Y-axis的线。

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

或旧版 Matlab

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

(如果你没有坐标轴句柄ax,你可以使用gca

这会在绘图的当前 XTick 处创建线条。如果你想让网格线有一个特定的范围,你必须改变 XTick

ax.XTick = 0:10:100;

如果你觉得线条太弱(难以看清),可以调高网格alha值(默认值为0.5)。

ax.GridAlpha = 1; %

或者给它上色

ax.GridColor  = 'r'; % Set grid color to red