- 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?
25 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Varun
el 20 de Mzo. de 2023
Editada: Varun
el 22 de Mayo de 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 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!