Search code examples
octave

Additional legend or text box window in a plot in octave


I would like to add to my plot a text or a legend box with comments.

At the moment my legend is plot at northeastoutside and i would like to add the new legend (or textbox) to the position southeastoutside.

Thanks!


Solution

  • Lacking more information about your case:

    To the best of my knowledge one axes object can only have a single legend object. You can create a second legend with a second axes object. Each legend will only list data elements associated with each axes. Adapted from Matlab Newsgroup thread

    a = [1:0.01:2*pi];  %create sample data
    b = sin(a);
    
    linehandle1 = line(a,b);  %creates a line plot with handle object
    axeshandle1 = gca;  % gets the handle for the axes object just created 
    legendhandle1 = legend('y = sin(x)', 'location', 'northeastoutside'); %makes first legend
    
    axeshandle2 = axes('Position',get(axeshandle1,'Position'),'xlim',get(axeshandle1,'xlim'),'ylim',get(axeshandle1,'ylim'),'Visible','off','Color','none'); %makes invisible axes with same position and scaling
    linehandle2 = line(pi/2,1,'Color','r','Marker','o','Parent',axeshandle2); %puts data set on 2nd axes
    linehandle3 = line(pi,0,'Color','b','Marker','x','Parent',axeshandle2);
    legend_handle2 = legend('peak','zero','location','southeastoutside'); %creates legend to go with 2nd axes
    

    figure created with second-axes method.

    If you just want text in that 2nd box, not necessarily legend info or data labels, you can play around with annotation as described above. This has the advantage of being simpler to call, but maybe harder to get the exact position/result you want. There are a large number of property options that can be adjusted to get the desired appearance. A few are shown in the example. It may be there are easier ways to set the size/position based on the legendhandle.

    a = [1:0.01:2*pi];  %create sample data
    b = sin(a);
    plot(a,b);
    legendhandle = legend('y = sin(x)','location','northeastoutside');
    annotation('textbox',[0.875 0.1 0.1 0.1],'string','my text','edgecolor','k','linewidth',1,'fitboxtotext','off');
    

    enter image description here