Change both transparency/opacity and marker size in legend of plot

22 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2021-5-5
编辑:Adam Danz 2021-5-5
Only one output to the legend function is documented. When you use additional undocumented outputs it causes all sorts of problems (example 1, example 2).
Instead of trying to change legend symbols, include a 'ghost' or a 'dummy' object that will appear in the legend but not on your axes.
The basic steps are to
  1. Copy the object using copyobj and set its XData or YData etc to NaN. Some objects may have different property names for their coordinates. This will make the object invisible.
  2. Set the graphics properties of the invisible objects according to how you'd like them to appear in the legend. This is where you can change marker size, transparency, etc. Also set the legend string using the DisplayName property.
  3. Produce the legend using the invisible object handles.
Example
fig = figure();
ax = axes(fig);
hold(ax,'on')
h1 = patch([0 0 .5 .5], [0 .5 .5 0], 'r');
h2 = scatter(rand(1,10), rand(1,10), 100, jet(10), 'filled');
% Copy selected objects
h1copy = copyobj(h1,ax);
h1copy.FaceAlpha = 0.3; % change transparency
h1copy.XData = nan(size(h1copy.XData)); % Now the object is invisisble
h1copy.DisplayName = 'Faded patch'; % assign legend string
h2copy = copyobj(h2,ax);
h2copy.Marker = 's'; % Change marker
h2copy.MarkerFaceColor = 'k'; % Change color
h2copy.DisplayName = 'Markers'; % assign legend string
h2copy.XData = nan(size(h2copy.XData)); % Now the object is invisible
% Create legend
legend([h1copy, h2copy],'Location','NorthEastOutside')

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by