Change both transparency/opacity and marker size in legend of plot
Show older comments
Hi all,
(using R2020b)
I am trying to set both an opacity level and custom marker size for my markers in my legend, but find that I cannot do both. The following code allows me to change the marker type, but I cannot find any way to set the MarkerAlpha to a preferred transparency:
[slopevstimefig_hleg, slopevstimefig_gleg] = legend(hand_scat([slopevstimefig_hleg_indx]),snnames_sorted,'Location','southeast','FontSize',22);
slopevstimefig_gleg_patch = findobj(slopevstimefig_gleg, 'type', 'patch'); %// objects of legend of type line
for i=1:size(snnames_sorted_mydata,2)
slopevstimefig_hleg.String{i} = ['\color[rgb]{' num2str(colormatrix_sorted(i,:)) '} ' slopevstimefig_hleg.String{i}]
end
set(slopevstimefig_gleg_patch, 'Markersize', 24); %// set marker size as desired
The above code seems to have erased the transparency information of my markers when saving the legend handles. If instead I just save the legend handle as:
slopevstimefig_hleg = legend(hand_scat([slopevstimefig_hleg_indx]),snnames_sorted,'Location','southeast','FontSize',22);
then my legend retains the proper associated transparencies, but there is no longer any way for me to change the Markersize as the findobj of type patch does not work on this handle. It is very strange to me that just changing which handles from the legend I store changes whether or not it retains the transparency for the markers in the legend, but was curious if anyone knows a way I could go about taking either approach and being able to both customize marker size in the legend and marker transparency individually for each item in the legend.
I've also created a brief test version of code that anyone can run without my data sets for testing purposes:
figure
blah = scatter(0,1,500,'filled')
set(blah, 'MarkerFaceAlpha', 0.25, 'MarkerEdgeAlpha', 0.25)
[test1 test2] = legend(blah)
The above code also removes the transparency of my marker when setting the legend when I store both the legend handle and its graphical handle. If instead I just store test1 = legend(blah), it retains transparency but I am unable to change marker size later.
Accepted Answer
More Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!