Grid a plot with diagonal lines

조회 수: 51 (최근 30일)
Srh Fwl
Srh Fwl 2020년 12월 28일
편집: Adam Danz 2023년 11월 22일
I would like to plot a diagonal grid where there are three or four lines of positive slope=1 like in the figure attached.
I don't see an equivalent of the command, "grid" that efficiently creates a horizontal–vertical grid (maybe someone knows of one?). So instead I'm trying to do it this way, where I avoid having to hard-code anything because I have many plots and will have to update them regularly.
xL = get(gca, 'XLim');
This gives me one line that goes through the origin, is positive, and has slope=1. However--silly question, I know--I can't figure out how to get additional lines that parallel the line. If anyone has any hints on this, I'd be grateful.

채택된 답변

Mischa Kim
Mischa Kim 2020년 12월 28일
There are a couple of things you could do. E.g.
x = -5:1:5; N = numel(x);
X = ones(N,1)*x;
Y = X + X';
  댓글 수: 1
Srh Fwl
Srh Fwl 2020년 12월 28일
Thank you, Mischa; exactly what I need.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 12월 28일
편집: Adam Danz 2023년 11월 22일
ConstantLine objects extend to the axis limits even when the limits change but as of r2020b, ConstantLine objects can only be vertical (xline) or horizontal (yline).
Here are two ways to create constant lines with a specific slope and y-intercept. They are "constant" because the lines update as the axis limits change. However, these lines are not ConstantLine objects and do not have labels and other features that ConstantLine objects have.
Use fimplicit
This is the simpler of the two solutions.
m = 1; % slope
b = 0; % y-intercept
h = fimplicit(@(x,y) m*x + b - y);
hold on
fimplicit(@(x,y) m*x + b+0.5 - y);
fimplicit(@(x,y) m*x + b-0.5 - y);
Use refline with a listener
refline creates the reference lines and a listener updates the lines when the axis limits change. Starting in MATLAB R2021a, you could use the LimitsChangedFcn instead of adding a listener.
% Set up demo
ax = axes;
hold(ax, 'on')
grid(ax, 'on')
% Add reference lines using refline([slope, y-intercept])
% Alternatively, you can use r=plot() or r=line() where each line is defined
% by two and only two coordinates (x0,y0),(x1,y1).
r(1) = refline(ax, [1,-.5]);
r(2) = refline(ax, [1, 0]);
r(3) = refline(ax, [1, .5]);
set(r, {'Color'}, {'r';'b';'g'}, 'LineWidth', 2)
% Add listener to update the reference lines when the
% axis limits are changed
addlistener(ax, {'XLim', 'YLim','XLimMode','YLimMode'}, 'PostSet', @(~,~)reflineListenerFcn(ax,r));
function reflineListenerFcn(ax,r)
% Responds to changes in xlim,ylim in axes 'ax'. Updates the
% xdata and ydata for reference lines 'r'.
r(~isvalid(r)) = [];
xdata = reshape([r.XData],2,[]);
ydata = reshape([r.YData],2,[]);
slope = diff(ydata,[],1) ./ diff(xdata,[],1);
yint = ydata(1,:) - slope.*xdata(1,:);
xl = xlim(ax);
yl = ylim(ax);
newY = slope'.*xl + yint';
newX = (newY-yint')./slope';


Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by