Clear Filters
Clear Filters

In-Figure Toolbar overlaps with the 1st legend line

5 views (last 30 days)
How it is possible to prevent an ovelap betwwen appearing fig toolbar (zoom/pan/etc) and the legend? It looks ugly. Everytime I hover over the figure, the toolbar apepars blocking the 1st line of the legend.
Ideally I would have the toolbar top-left, since my legent is always north-east. But not sure how to do that. Obviously, I don't wanna disable the toolbar, and I cannot move the legend from north-east.
I was thinking also to play with the legend:
  • add an empty entry on top (the overlap region)--did not work for me, I think legend cannot have an entry w/o a line;
  • finally, move the legend down a notch, using its Position property; this is not trivial, since I need to set all 4 values for Position, including width and height of the legend, which are dynamic (diffent resolutions, Mac & Win... I'm worried that my math won't be robust). Position property sets lower-left corner of the legend... I'm not confident to calcualate these (as well as width and height) for various legend compositions (this is my app, where diffeent lines can be selected; the legend is not static).
  • finally, I hacked the 2nd coordinate of Position (bottom of the legend) to dip it a notch, while keeping the other 3 numbers the same; no visible legend shaking on screen. The trick is to keep Location = 'northeast' in legend call, right before the hack (othwerwise, on subsequent legend calls it won't update, since touching Position sets Location to none). I wonder if there is a better way of preventing an overlap.
leg = legend(ax, Location='northeast');
leg.Position(2) = leg.Position(2) - 0.02; % hack the bottom

Answers (2)

Simon Chan
Simon Chan on 25 Feb 2023
Add dummy data to make empty rows in legend for your consideration.
If the dummy data mess up with your original data, then this is not the solution for you.
plot(ax,NaN,NaN,'w'); % 1st dummy data, should use black color in your case
hold on
plot(ax,NaN,NaN,'w'); % 2nd dummy data, should use black color in your case
leg = legend(ax);

Image Analyst
Image Analyst on 25 Feb 2023
Try moving the legend outside the plot box:
leg = legend(ax, Location='eastoutside'):




Community Treasure Hunt

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

Start Hunting!