Plot Legend and "hold on" issue

24 次查看(过去 30 天)
Andrew Feenan
Andrew Feenan 2023-8-18
回答: Yash 2023-8-31
Hi,
I am having some issues with the plot legend in App Desinger.
I want to be able to plot a number of data sets on a single probability plot (this works with the code below), however, when I change data set variables and plot again, the data displays on the plot but the legend is overwritten with only the new data set legend.
For example, the code below goes in the plotbuttonpushed callback, so when I select a few checkboxes and press the "Plot" button, the data and the correct legend is plot as it should be. However, when I change some varibales in the data set, I select more checkboxes to plot more data and press plot again, the data is overlayed on to the plot correctly, but the legend is overwritten with the new data.
Is there anyway I can solve this?
% Filter data based on the checkboxes
plot_data = table2array(app.split_table_data);
selected_data = plot_data(:, app.UITable.Data{:, 2});
% Plotting
line_handles = probplot(app.UIAxes, selected_data, "noref");
set(line_handles, 'LineWidth', 1, 'Color', app.plot_colours(random_number,:));
xlabel(app.UIAxes, xAxisLabel);
title(app.UIAxes, 'Probability Plot');
% Generate new legend labels for the current data
if strcmpi(app.Technology_Drop_Down.Value, '40nm')
new_legend_labels = strcat(app.device_names(app.UITable.Data{:, 2}), " Temp: ", app.Temperature_Drop_Down.Value);
else
new_legend_labels = app.device_names(app.UITable.Data{:, 2});
end
if ~isfield(app, 'master_legend_handles')
app.master_legend_handles = [];
app.master_legend_labels = {};
end
% Append the new handles and labels to the master lists
app.master_legend_handles = [app.master_legend_handles; line_handles];
app.master_legend_labels = [app.master_legend_labels, new_legend_labels];
% Display the legend
legend(app.UIAxes, app.master_legend_handles, app.master_legend_labels, 'Location', 'Best');
hold(app.UIAxes, "on");
app.UITable.ColumnEditable = [false, false];
app.nSigma_Sigma_Edit_Field.Value = 0;

回答(1 个)

Yash
Yash 2023-8-31
It seems like you're trying to update a legend in an App Designer GUI in MATLAB when you add new data sets to your probability plot. The issue you're facing is that when you plot new data, the legend is overwritten with the legend for the new data set. To solve this issue and maintain a cumulative legend, you can make the following modifications to your code:
  1. Initialize the legend handles and labels arrays outside of your callback function. You should do this in the App Designer's startupFcn, so they persist between callback invocations.
  2. In your callback function, update the legend with all the handles and labels for all the data sets currently plotted.
Here's how you can modify your code:
In the App Designer, go to the startupFcn to initialize the legend handles and labels:
function startupFcn(app)
% Initialize the legend handles and labels
app.master_legend_handles = [];
app.master_legend_labels = {};
end
Now, in your callback function:
function PlotButtonPushed(app, event)
% Filter data based on the checkboxes
plot_data = table2array(app.split_table_data);
selected_data = plot_data(:, app.UITable.Data{:, 2});
% Plotting
line_handles = probplot(app.UIAxes, selected_data, "noref");
set(line_handles, 'LineWidth', 1, 'Color', app.plot_colours(random_number,:));
xlabel(app.UIAxes, xAxisLabel);
title(app.UIAxes, 'Probability Plot');
% Generate new legend labels for the current data
if strcmpi(app.Technology_Drop_Down.Value, '40nm')
new_legend_labels = strcat(app.device_names(app.UITable.Data{:, 2}), " Temp: ", app.Temperature_Drop_Down.Value);
else
new_legend_labels = app.device_names(app.UITable.Data{:, 2});
end
% Append the new handles and labels to the master lists
app.master_legend_handles = [app.master_legend_handles; line_handles];
app.master_legend_labels = [app.master_legend_labels, new_legend_labels];
% Display the legend
legend(app.UIAxes, app.master_legend_handles, app.master_legend_labels, 'Location', 'Best');
hold(app.UIAxes, "on");
app.UITable.ColumnEditable = [false, false];
app.nSigma_Sigma_Edit_Field.Value = 0;
end
By initializing and updating the app.master_legend_handles and app.master_legend_labels outside of your callback, the legend will accumulate all the data sets you've plotted, and it won't be overwritten when you plot new data.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by