필터 지우기
필터 지우기

xline - draw a partial line

조회 수: 30 (최근 30일)
dormant
dormant 2024년 1월 10일
댓글: Image Analyst 2024년 1월 12일
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
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
ans = 2×1 logical array
1 0
% 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
ans = 2×1 logical array
0 1
dormant
dormant 2024년 1월 11일
That doesn't work with my data. But thanks for the suggestion.

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

채택된 답변

Dyuman Joshi
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
X = 1×10
10.0000 5.0000 7.5000 8.5000 2.5000 5.0000 1.5000 8.5000 9.5000 4.0000
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
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
X = 1×10
2.0000 0.5000 5.0000 7.5000 4.5000 5.0000 0.5000 5.0000 9.5000 9.0000
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
h =
10×1 Line array: Line Line Line Line Line Line Line Line Line Line
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
X = 1×10
2.0000 0.5000 5.0000 7.5000 4.5000 5.0000 0.5000 5.0000 9.5000 9.0000
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
h =
Line with properties: Color: [0 0 0] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [2 2 NaN 0.5000 0.5000 NaN 5 5 NaN 7.5000 7.5000 NaN 4.5000 4.5000 NaN 5 5 NaN 0.5000 0.5000 NaN 5 5 NaN 9.5000 9.5000 NaN 9 9 NaN] YData: [-0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN -0.2000 0.3000 NaN … ] (1×30 double) Use GET to show all properties

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

추가 답변 (3개)

Mathieu NOE
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

Image Analyst
Image Analyst 2024년 1월 10일
You can use line or plot instead of xline

dormant
dormant 2024년 1월 11일
I decided on an alternative approach. I plotted the data again with another axis with tick marks where my xlines would be.
I'm not sure the axis properties are set optimally, but this does the job.
x = 1:100;
y = sin(x/10);
figure;
t = tiledlayout(1,1);
ax1 = axes(t);
plot(x,y,'k-');
ax2 = axes(t);
plot(ax2,x,y,'k-');
ax2.XAxisLocation = 'top';
xtick( [7 8 9 12 73 84] );
ax2.XTickLabel = {};
ax2.TickDir = 'in';
ax2.XColor = 'r';
ax2.YTick = [];
ax2.XAxisLocation = 'top';
box( ax2, 'XColor', 'k', 'YColor', 'k' );
ax2.TickLength = [0.1 0];
% Link the axes
linkaxes([ax1 ax2], 'x')
  댓글 수: 1
Image Analyst
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by