xline - draw a partial line
44 次查看(过去 30 天)
显示 更早的评论
Is there an easy way to get an xline plot for an array of X values, but with the line only taking up the first or last 10% of the Y axis? It's for this plot, where plotting the whole line is obscuring the blue data too much..
This is for a script where the Y axis is logarithmic and its values are not always the same. I could plot the lines in a loop, having calculated the appropriate Y values. But is there a simpler way?
3 个评论
Voss
2024-1-10
编辑:Voss
2024-1-10
@dormant: How about putting the black vertical lines beneath the blue line, so that the blue line is not obscured? That can be done: (1) by plotting the black lines before the blue line, or (2) by reordering the lines in the axes Children property after they are plotted, e.g. using uistack or setting the Children property directly.
Example:
% plot a thick line and a thin line on top:
figure()
h_thick = plot(1:10,'LineWidth',10);
hold on
h_thin = plot(1:10,'LineWidth',2);
% thin line is 1st in axes Children (most recently created):
ax = gca();
ch = get(ax,'Children');
ch == h_thin
% new figure and axes (only necessary for demonstration in Answers):
ax = copyobj(ax,figure());
% get the new handle of the thin line (also only necessary for
% demonstration in Answers):
ch = get(ax,'Children');
h_thin = ch(1);
% reverse the order of the axes Children:
set(ax,'Children',flip(get(ax,'Children')));
% now the thin line is underneath the thick one,
% and the thin line is last in Children:
ch = get(ax,'Children');
ch == h_thin
采纳的回答
Dyuman Joshi
2024-1-10
You will have to do that manually -
%Sinosuidal data for example
x = 0:0.01:10;
y = sin(x);
plot(x, y, 'LineWidth', 2)
%Generate random values to put xlines
X = randi(20, 1, 10)/2
y1 = -0.2;
y2 = 0.3;
%Draw xlines for the values in X, extending from y1 upto y2
hold on
plot([X; X], [y1; y2], 'k')
1 个评论
Voss
2024-1-10
@dormant: Note that this method produces one line object for each x value. As you can see below h is an array of 10 lines:
figure; rng(11)
% Sinosuidal data for example
x = 0:0.01:10;
y = sin(x);
plot(x, y, 'LineWidth', 2)
% Generate random values to put xlines
X = randi(20, 1, 10)/2
y1 = -0.2;
y2 = 0.3;
% Draw xlines for the values in X, extending from y1 up to y2
hold on
h = plot([X; X], [y1; y2], 'k') % 10 lines
Having too many graphics objects that MATLAB needs to render can make your GUI sluggish to interact with. Therefore, in general it's a good idea to minimize the number of graphics objects you create.
To that end, you can achieve the same effect as above with a single line object by taking advantage of how NaNs are represented in lines (NaNs make gaps). Here's the same example, but with one line object for all vertical lines:
figure; rng(11)
% Sinosuidal data for example
x = 0:0.01:10;
y = sin(x);
plot(x, y, 'LineWidth', 2)
% Generate random values to put xlines
X = randi(20, 1, 10)/2
y1 = -0.2;
y2 = 0.3;
% Draw xlines for the values in X, extending from y1 up to y2
hold on
N = numel(X);
X_plot = [X; X; NaN(1,N)];
Y_plot = repmat([y1; y2; NaN],1,N);
h = plot(X_plot(:),Y_plot(:), 'k') % one line
更多回答(3 个)
Mathieu NOE
2024-1-10
hello
I am not aware that a special version of xline exist , but you can do your own special xline like that (a very crude and simple code that you can save as a function for future massive use) :
simply try with different a and b values to adjust the amount of blank around the data curve
% dummy data
n = 1000;
t = (0:n-1)/n;
y = 0.1 + sin(pi*t/max(t)).^3.*abs(sin(2*pi*10*(t+1*t.^3))); % signal
[PKS,LOCS] = findpeaks(y,t,'SortStr','descend','NPeaks',15);
figure(1)
ylimits = [1e-2 1e1];
semilogy(t,y,'b',LOCS,PKS,'.r','linewidth',2,'markersize',25);grid on
% add my special vertical lines
a = 0.25; % factor on the lower portion (must be < 1)
b = 1.25; % factor on the upper portion (must be > 1)
hold on
for k = 1:numel(PKS)
plot(LOCS(k)*ones(2,1), [ylimits(1) a*interp1(t,y,LOCS(k))],'--k','linewidth',0.5);
plot(LOCS(k)*ones(2,1), [b*interp1(t,y,LOCS(k)) ylimits(2)],'--k','linewidth',0.5);
end
ylim(ylimits);
hold off
0 个评论
dormant
2024-1-11
1 个评论
Image Analyst
2024-1-12
Looks confusing to me unless it's explained somewhere. The red lines don't go to the curve but all go to some constant y value. And the x location of them seems random.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!