I run
loglog(x,y);
legend('First script');
from the first script.
Then, hold on;
.
I run a similar second script.
I see two graphs correctly, but only the initial legend.
I want to increment the legend from different scripts.
How can you add to the legend from a single script?
Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend. Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels.
To move the legend to a different tile, set the Layout property of the legend. Determined by Position property. Use the Position property to specify a custom location.
Answers (1) The warning "Ignoring extra legend entries" happens if you call legend() with more names than there are lines in your axes. In this case, it is related to your use of "hold on".
Another possible way to do this and it is called 'DisplayName':
hold all
for ind=1:3
x=[0:0.1:10];
plot(x, sin(x)+ind, 'DisplayName',['sin + ' num2str(ind)]);
end
legend(gca,'show')
Just use this property when plotting from wherever to whatever axes :)
There are even more things possible, like dynamic legend!
For more information see this: dynamic legend from undocumented matlab
EDIT
you have to update the legend after every run of a script. you can do this with the dynamic legend or by just legend('off'); legend('show')
The easiest way to deal with this is to save the handle to the legend when you create it, then whey you are ready to update the plot with a new legend with another series included, delete the legend and make a new one:
legendStrings = {'First script'};
h_legend = legend(legendStrings{:});
% ... computations, hold on and additional plot on axis
delete(h_legend);
legendStrings{end+1} = 'Second script';
h_legend = legend(legendStrings{:});
% rinse and repeat...
Usually with graphics objects, such as a textbox, I would say just reuse the object via the handle (don't delete
). However, if you update the legend instead of replacing it, you have to worry about more than just the strings. The MathWorks solution referenced by zroth actually seems to address this approach!
As an alternatively to delete
and create new, you can also toggle the legend on and off with legend('off'); legend('show');
as the answer in Eugenio's comment suggests.
The simplest way is:
hold all;
p1=plot(1:10,1:10);
legend(p1,'1'); % in this way plot(x,y) is saved as variable p1 with name '1'
p2=plot(1:10,11:20); % different data set
legend(p2,'2');
legend(gca,'off');
legend('show');
This is connection of few methods, it's very simple and it could be use everywhere.
I had a similar issue: I plotted three sets of experimental data first, then got into my parameter estimation to simulate the function and wanted to plot the model data every time, holding on to the experimental data but deleting the model data from the previous run. And I wanted the legend to show that. I was able to do it with a combination of solutions from different questions.
Initial commands (top of my main)
close all
will ensure your plot starts anew every time
First plot (in my main)
plot(points,expdata1,'ro','DisplayName','Experimental, L= 0.1 m'); hold on
plot(points,expdata2,'bo','DisplayName','Experimental, L= 0.2 m');
plot(points,expdata3,'go','DisplayName','Experimental, L= 0.3 m');
legend('-DynamicLegend','Location','Best')
drawnow
h_old=plot(0,250);
drawnow
forces the plot to be drawn right away, and h_old
is just a "placeholder" that I make use of later on. I chose 0,250 because it's in the range of the data (otherwise it messes up the axis)
Second Plot (in the called function)
h(1)=plot(points,modeldata1,'r-','DisplayName','Model Data, L= 0.1 m');
h(2)=plot(points,modeldata2,'b-','DisplayName','Model Data, L= 0.2 m');
h(3)=plot(points,modeldata3,'g-','DisplayName','Model Data, L= 0.3 m');
delete(h_old);
h_old = h;
drawnow
I delete h_old
and overwrite it with the new plots I created. That way, at the second iteration the plot from the 2nd iteration will be plotted, the plot from the 1st will be deleted and after these operations I get it to display the plot (again drawnow
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With