Matlab legend show opaque plot symbols when plot symbols are semi-transparent.
    3 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Kai
 le 1 Fév 2024
  
    
    
    
    
    Réponse apportée : Pratyush Swain
      
 le 1 Fév 2024
            HI, I am trying to create a scatter plot cloud, of which I set the points are all "filled" and semi-transparent by setting "MarkerFaceAlpha=0.1". However, when I export the legend automatically, the point symbols are also the same semi-transparent and it is hardly can be seen in legend. Do we have an easy way to switch on/off the MarkerFaceAlpha?
Here is the code:
figure()
% plot the AIS cargo and fishing data
scatter(X1,Y1,16, [0.9290, 0.6940, 0.1250],"filled",MarkerFaceAlpha=0.1,DisplayName='A')
hold on
scatter(X2, Y2,16, [0 0.4470 0.7410],"filled",MarkerFaceAlpha=0.1,DisplayName='B')
hold off
legend(Location="southwest",Box="off")
0 commentaires
Réponse acceptée
  Pratyush Swain
      
 le 1 Fév 2024
        Hi kai,
I understand while creating a semi-transparent scatter plot, the markers in legend can become difficult to realize too. However, you can work around this limitation by creating a "dummy" scatter plot with fully opaque markers solely for the purpose of displaying them in the legend. Here's how you can modify your code to achieve this:
% Generate some dummy data for the scatter plot
rng(0);
X1 = randn(100, 1) + 5; % Generate 100 random points around X=5
Y1 = randn(100, 1) + 5; % Generate 100 random points around Y=5
X2 = randn(100, 1) - 5; % Generate 100 random points around X=-5
Y2 = randn(100, 1) - 5; % Generate 100 random points around Y=-5
% Create the scatter plot
figure();
scatter(X1,Y1,16, [0.9290, 0.6940, 0.1250], "filled", 'MarkerFaceAlpha', 0.1);
hold on;
scatter(X2, Y2,16, [0, 0.4470, 0.7410], "filled", 'MarkerFaceAlpha', 0.1);
% Create dummy plots for the legend
hA = scatter([],[],16, [0.9290, 0.6940, 0.1250], "filled", 'DisplayName', 'A');
hB = scatter([],[],16, [0, 0.4470, 0.7410], "filled", 'DisplayName', 'B');
hold off;
legend([hA, hB], 'Location', "southwest", 'Box', "off");
In this code, hA and hB are handles to the dummy scatter plots with opaque markers for the legend. The dummy scatter plots are created with empty data so they don't actually appear on the plot, but they are used to generate the legend entries.
For more information on "scatter" function, please refer to https://www.mathworks.com/help/matlab/ref/scatter.html 
Hope this helps.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Legend dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


