0

I want to draw a vertical line for the max Value of y(hier for x near 20), how to do that? (i mean downwards from this value (x=20, ymax) to (x=20, y=0))

x = logspace(0,5);
y=[0;0;0;0;0;0;0;0.15;0.34;0.51;0.71;0.84;0.88;0.856;0.75;0.63;0.47;0.36;0.32;0.33;0.37;0.45;0.5;0.51;0.48;0.4;0.33;0.22;0.14;0.07;0.02;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0];
y=y';

figure(5) 
plot(x,y,'-o','LineWidth',2);
%ax(1) = axes('Position',[0.35 0.13 0.3 0.8]); 
xlim([0,10000]); 
ylim([0,1]);
set(gca, 'XScale', 'log')

grid on
grid minor
set(gca,'FontSize', 20);
set(gca,'Linewidth',1.8);

title('\rm Verteilung der Porengrößen');
xlabel('Porengröße [{\fontsize{24}\mu}{\fontsize{20}m}]');
ylabel('Amplitude des NMR-Signals [V]');
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Bobotic
  • 43
  • 1
  • 9
  • 2
    You've not accepted answers in your previous questions. e.g [**this**](https://stackoverflow.com/questions/44248618/how-to-make-fontsize-bigger-for-one-letter-in-matlab-plot-title) and [**this**](https://stackoverflow.com/questions/38228889/convert-a-double-array-to-unsigned-short). If the answers solve your problem, consider marking the most helpful one as [accepted](http://meta.stackexchange.com/a/5235/335102) by clicking **`✔`**, on the left side of the answers. You can also [upvote](http://meta.stackexchange.com/a/173400/335102) if u like by clicking **`▲`** on the left side of the answers – Sardar Usama Jun 09 '17 at 02:31
  • 1
    Possible duplicate of [I cannot figure out how to draw horizontal and vertical lines in matlab](https://stackoverflow.com/questions/28334660/i-cannot-figure-out-how-to-draw-horizontal-and-vertical-lines-in-matlab) and [vertical refline in matlab](https://stackoverflow.com/questions/17798093/vertical-refline-in-matlab) – EBH Jun 09 '17 at 06:29

2 Answers2

4

Retain the previous plot using hold on. Find the maximum value of y and the value of x for which y is maximum and plot the vertical line like this:

hold on;
plot([x(y==max(y)); x(y==max(y))], [0; max(y)]);
hold off;

which gives:

output

This works even if you have multiple maximum points. Here is an output for another random data with 3 maximum points:

output2

As you can see, this makes lines with different colors. If you want the same colors, use the 'Color' property of plot,

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
3

You could use stem.

% ... your code
%
[maxy, idx] = max(y); % Get maximum y and its index
hold on               % use hold on to plot again on same axes
% vertical stem plot, various marker options available: see docs
stem(x(idx), maxy, 'marker', 'none', 'linewidth', 1.8)

Output:

one vertical line


This code plots a vertical line at the first maximum. If there is a chance that you will have more than one equal maximum value, then you should use something like this:

idx = (y == max(y));  % Get *all* logical indices of max values
hold on  
% Plot vertical lines at all max points
stem(x(idx), y(idx), 'marker', 'none', 'linewidth', 1.8); 

multiple vertical lines

Wolfie
  • 27,562
  • 7
  • 28
  • 55