How do I draw a number of arbitrary grid lines from data in arrays, independently from the default grid?
4 views (last 30 days)
Show older comments
Leonardo Silveira Kiepper
on 5 Jul 2020
Commented: Leonardo Silveira Kiepper
on 6 Jul 2020
I'm trying to create a plot with a single diagonal gridline.
I've tried to solve this by myself, and by researching, on a number of occasions, but because of time constraints I've always went the easy way. Here's what I mean:
clc; clear all; close all; format compact; format shortG;
CHCs=(0:0.01:1)';
a=9; % Quantos HPS avaliar?
b=0.5; % HPS mínimo
c=500; % HPS máximo
if a==1
HPS=b
else
r=nthroot(c/b,a-1);
HPS=b*r.^(0:a-1)
end
CHCmedio=CalculaCHCmedioBP(CHCs,HPS);
% Generates strings for legend
LegendString=strings(size(HPS));
for k=1:length(HPS), LegendString(k)=string(sprintf('HPS = %5.3f',HPS(k))); end
% Set figure size in normalized units
f=figure; set(f, 'Units', 'Normalized', 'OuterPosition', [0.25, 0.1, 0.5 ,0.8])
% Plots a diagonal grid line. 'HandleVisibility' 'off' makes the legend
% funcion skip this entry
plot([0,1],[0,1],'--','color',[1 1 1]*223/255,'HandleVisibility','off');
axis([0, 1, 0, 1]); axis square; grid on;
% Plots data
hold on; plot(CHCs,CHCmedio); legend(LegendString,'Location','southeast')
To date, this is my best effort to date at creating an arbitrary grid line. Here's the figure it generates:

This approach has a few problems.
- The diagonal line can be picked by the legend function. This was compensated for by turning 'HandleVisibility' 'off';
- The lines' data points can be picked by the data cursor, as shown in the figure.
In a practical sense, this means that I need to compensate for problems I've created by using a workaround, which makes the code more complex than it could be. In a theoretical sense, it's just an incorrect use of the built in functions, because I've plotted the grid line in the same "context" as the data I'm interested in, even though the diagonal grid line shows no relevant data by itself, and is intended to be just a visual aid.
An ideal solution should make this diagonal line just as interactive as the other grid lines (which is, not interactive at all), and not need any form of compensation like the ones I've described, or similiar. And it's also worth pointing out that, while this case shows a very simple custom grid line, it'd be desirable to be able to plot more complex curves (like the actual data) as grid lines, simpliy by adding more data points do the arrays from which the grid lines should be plotted, such as would be possible by the plot approach.
An extended version of this problem is to plot any number of arbitrary grid lines. With the plot approach, this could be done by writing "plot(X,Y)" if you make X a column array of size 'n', and Y a matrix of size 'n' by 'm', to plot 'm' curves.
I know this is something that should be posssible to do, as I remeber using the Nichols chart before in a school project, and that has a very complex grid which is not interactive.
1 Comment
jonas
on 5 Jul 2020
Interesting. I don't have a good answer, but you can solve the second issue through the properties "HitTest" and/or "PickableParts"
Accepted Answer
dpb
on 5 Jul 2020
The trick used there (Nichols plot and other response plots) is to tag them all when drawing and then them all to be hard for user to find via something like--
GridHandles(1,1) = line(phase, gain, Zlevel(ones(length(gain),1),:), ...
'XlimInclude',LimInclude,'YlimInclude',LimInclude,...
'LineStyle', ':', 'Color', Color, ...
'Parent', ax, 'Tag', 'CSTgridLines', ...
'HandleVisibility', 'off', 'HitTest', 'off');
After drawing with such and setting the handle visibility and hit test properties (jonas got the one right, +1 there! :) ) then they'll not be subject to user interaction.
I would think that would solve your problems pretty well..
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!