In Matlab, how to draw lines from the curve to specific xaxis position?

前端 未结 1 1361
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 09:28

I have a spectral data (1000 variables on xaxis, and peak intensities as y) and a list of peaks of interest at various specific x locations (a matrix called Peak) which I obtain

相关标签:
1条回答
  • 2021-01-25 09:38

    Simple annotation:

    Here is a simple way to annotate the peaks:

    plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
    

    where x and y is your data, and x_peak and y_peak is the coordinates of the peaks you want to annotate. The add of 0.1 is just for a better placing of the annotation and should be calibrated for your data.
    For example (with some arbitrary data):

    x = 1:1000;
    y = sin(0.01*x).*cos(0.05*x);
    [y_peak,x_peak] = PeakDetection(y); % this is just a sketch based on your code...
    plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
    

    the result:


    Line annotation:

    This is just a little bit more complicated because we need 4 values for each line. Again, assuming x_peak and y_peak as before:

    plot(x,y);
    hold on
    ax = gca;
    ymin = ax.YLim(1);
    plot([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'r')
    % you could write instead:
    % line([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'Color','r')
    % but I prefer the PLOT function.
    hold off
    

    and the result:


    Arrow annotation:

    If you really want those arrows, then you need to first convert the peak location to the normalized figure units. Here how to do that:

    plot(x,y);
    ylim([-1.5 1.5]) % only for a better look of the arrows
    peaks = [x_peak.' y_peak.'];
    ax = gca;
    % This prat converts the axis unites to the figure normalized unites
    % AX is a handle to the figure
    % PEAKS is a n-by-2 matrix, where the first column is the x values and the
    % second is the y values
    pos = ax.Position;
    % NORMPEAKS is a matrix in the same size of PEAKS, but with all the values
    % converted to normalized units
    normpx = pos(3)*((peaks(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
    normpy = pos(4)*((peaks(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
    normpeaks = [normpx normpy];
    for k = 1:size(normpeaks,1)
        annotation('arrow',[normpeaks(k,1) normpeaks(k,1)],...
            [normpeaks(k,2)+0.1 normpeaks(k,2)],...
            'Color','red','LineWidth',2)
    end
    

    and the result:

    0 讨论(0)
提交回复
热议问题