- You can pop-out the figure into a figure window and manually toggle (turn off and on) the legend once. The refreshed legend will show up with information regarding both plots in the same legend box. However, this does not reflect on the plot in the live editor.
How do I create multiple group scatter plots with the same legend?
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a table with outputs from multiple model Runs. I ran this model for 24 rivers, with different input datasets. Now I want to create a scatterplot, showing the range of values for each river, by plotting the minimum and maximum value for each river. I achieved this, but now I want to show in which run each of these minimum or maximum values was produced. I wanted to do this using gscatter, and using the Run Name as group variable. However, because I am using gscatter twice (once for the minimum values, and then again for the maximum values), and the Run Names differ for each scatterplot (the minimum values were often produced by different Runs than the maximum values), the legend only shows the colors used for the last scatterplot (for the maximums) that was created, and thus doesn't show the correct values for the first scatterplot (the minimums). Below is the code I am using
X=outputtable.Orig;
Y1=min([outputtable{:,2:9},outputtable{:,11:12}]')'; %find the minimum values for the relevant columns
Y2=max([outputtable{:,2:9},outputtable{:,11:12}]')'; %find the maximum values for the relevant columns
for i=1:height(outputtable)
minloc(i)=find(outputtable{i,2:13}==Y1(i))+1; %find the column in which the minimum value is located, using +1 because of table indices {:,2:13}
maxloc(i)=find(outputtable{i,2:13}==Y2(i))+1; %find the column in which the maximum value is located, using +1 because of table indices {:,2:13}
end
minvar=string(outputtable.Properties.VariableNames(minloc)); %find the variable name (=Run Name) corresponding to the minimum value per river
maxvar=string(outputtable.Properties.VariableNames(maxloc)); %find the variable name (=Run Name) corresponding to the maximum value per river
gscatter(X,Y1,outputtable.MinVar)
hold on
gscatter(X,Y2,outputtable.MaxVar)
0 Kommentare
Antworten (1)
Varun
am 20 Mär. 2023
Bearbeitet: Varun
am 22 Mai 2023
Hello!
As per my understanding, you’re facing an error with the auto-generated legend for the plots generated on the same axes. I tried plotting similar plots using some sample data from a MATLAB example. This is what helped me fix the issue:
2. You can specify the legend in the code itself. Since you are using the variables “outputtable.MinVar” and “outputtable.MaxVar” as the grouping variables for the two plots, their unique values will show up in the legend. So, this code snippet should do the job:
legend(string([unique(outputtable.MinVar), unique(outputtable.MaxVar)]))
This will add the required legend to the plot. Hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!