Grid a plot with diagonal lines
30 views (last 30 days)
Show older comments
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');
plot(xL,xL);
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.
0 Comments
Accepted Answer
Mischa Kim
on 28 Dec 2020
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';
plot(X',Y');
grid
More Answers (1)
Adam Danz
on 28 Dec 2020
Edited: Adam Danz
on 22 Nov 2023
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/472762/image.gif)
% Set up demo
figure
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';
set(r,{'XData'},mat2cell(newX,ones(size(newX,1),1),2),...
{'YData'},mat2cell(newY,ones(size(newY,1),1),2))
xlim(ax,xl)
ylim(ax,yl)
end
0 Comments
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!