Search code examples
matlabplotgraphmatlab-figurecdf

How to display statistics as text in a graph?


I am using cdfplot in matlab to plot the empirical CDF of certain quantities.

[h,stats]=cdfplot(quantity)

Now stats returns me a structure having min, max, mean etc. I want these values to be displayed as text in the graph.

I have lot of similar graphs to plot and do not want to do it manually.


Solution

  • To put a text on a plot you use the text function. Here is a quick example on one plot:

    y = evrnd(0,3,100,1);
    [h, stats] = cdfplot(y);
    hold on
    x = -20:0.1:10;
    f = evcdf(x,0,3);
    plot(x,f,'m')
    legend('Empirical','Theoretical','Location','NW')
    stat_type =  {'min: ';'max: ';'mean: ';'median: ';'std: '}; % make titles
    stat_val = num2str(struct2array(stats).'); % convert stats to string
    text(-15,0.7,stat_type) % <-- here
    text(-11,0.7,stat_val) % <-- and here
    hold off
    

    This will give:

    CDF_text

    and you can use it inside a loop to do it for all the graphs.

    The tricky thing is to define where to put the text on the graph. Here I choose (-15,0.7) and (-11,0.7) as a fixed points where I know there is no data. You should look at your plots, and find the correct place for that.