Disappearing Legend: Saving a figure with a legend as a .fig file
5 次查看(过去 30 天)
显示 更早的评论
I am trying to save a figure with a legend positioned manually outside of the plot as a .fig file. When I generate the figure the legend is positioned exactly as I want it. I then save the file using the figure GUI, but when I reopen the saved figure the legend has "disappeared".
I'm fairly certain that this has something to do with the parent/child relationship between figures, axes and legends, but I'm a beginner in understanding the structure of the these objects in Matlab.
I am including a minimal example of what I am trying to do. I am generating the figure and legend in a loop because this is a necessary part of my "real" code. Any help or insight in where to look for an answer to this coding question would be appreciated.
%Problem Code
clear all;
fig1 = figure;
set(fig1,'units','centimeters','Position',[0 0 28 15]);
h_axes_1=axes('parent',fig1,'units','centimeters','Position',[3 3 13 9]);
hold on
for i = 1:1:5
plot(h_axes_1,[1:10],2*i*[1:10]);
Legendinfo{i} = ['i =' num2str(i)];
end
leg1 = legend(Legendinfo,'Units','centimeters','parent',fig1,'Position',[17 6 6 6]);
hold off
If I change the line of code that constructs the legend to
legend(Legendinfo,'Units','centimeters','parent',fig1,'Location','northeastoutside');
opening the saved file produces the same figure as the original file. That is, the issue of the disappearing legend is resolved.
While this is a "work around" I would much prefer being able to specify the exact location and size of my legend.
I am using release 2015b. This problem occurs whether I select the drop down "save" option from the file menu in the figure window or I use the "savefig" command in the command window.
Thanks.
2 个评论
Walter Roberson
2016-9-22
Which MATLAB release are you using?
And please show your command to save the plot.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!