legend symbol disappears when plotting a polyshape

If there is not any output from the legend, the symbol of the polyshape is visible:
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
legend(p);
Instead, if I try to get both outputs of the legend with a polyshape, the symbol of the polyshape disappears:
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
[h,objh]=legend(p);
How can I still see/visualise the symbol of the polyshape, when I need to get both outputs of the legend?

댓글 수: 5

Sim
Sim 2024년 6월 28일
Hi, any suggestion? :-)
Adam Danz
Adam Danz 2024년 6월 28일
편집: Adam Danz 2024년 6월 28일
How are you using the second output? Why do you need it? I'm interested.
As @Star Strider mentioned, additional outputs after the first output to legend are not documented and have led to all sorts of issues. The option to request additional outputs to legend will be removed in the future.
@Adam Danz — Some of that capability — among which are the ability to change the marker and line properties only in the legend — needs to be preserved somehow, if the second output is eliminated. Perhaps those can be part of the Children field, or even a new field. Eliminiating the second output could break a lot of existing code.
Hi @Adam Danz! Sorry for my late reply.... :-) I would need the second output to increase the marker/symbol size of other plots in the legend, as explained in How can I change the marker size in legend?. Something like this:
hold on
s=plot(rand(1,5),rand(1,5),'o','MarkerSize',8,'MarkerFaceColor','b');
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
[h,objh]=legend([p s]);
% increase the markers size
icons=findobj(objh,'Marker','o');
set(icons,'MarkerSize',15);
Thanks for share the use cases @Sim & @Star Strider.

댓글을 달려면 로그인하십시오.

 채택된 답변

I don’t have any specific suggestions.
It works with one output, however not more than one. That the legend function has more than oue output is not documented, nor are the behaviours associated with the additional outputs. The second output (‘objh1’ here) can be used to change the marker properties, however without markers, there is nothing to change. (Experimenting with its properties didn’t bring back the patch in the legend.) Since the additional outputs are undocumented, you will likely need to Contact Support and ask about this. Also, ask for published documentation on the additional outputs be made freely available.
Include the URL of this thread in your note to MathWorks so you don’t have to repeat all this.
figure
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
h = legend(p);
get(h)
AutoUpdate: on BackgroundAlpha: 1 BeingDeleted: off Box: on BusyAction: 'queue' ButtonDownFcn: @bdowncb Children: [0x0 GraphicsPlaceholder] Color: [1 1 1] ContextMenu: [1x1 ContextMenu] CreateFcn: '' DeleteFcn: '' Direction: 'normal' DirectionMode: 'auto' EdgeColor: [0.1500 0.1500 0.1500] FontAngle: 'normal' FontName: 'Helvetica' FontSize: 9 FontWeight: 'normal' HandleVisibility: 'on' HitTest: on Interpreter: 'tex' Interruptible: off ItemHitFcn: @defaultItemHitCallback Layout: [0x0 matlab.ui.layout.LayoutOptions] LineWidth: 0.5000 Location: 'northeast' NumColumns: 1 NumColumnsMode: 'auto' Orientation: 'vertical' Parent: [1x1 Figure] PickableParts: 'visible' Position: [0.7327 0.8568 0.1529 0.0439] Selected: off SelectionHighlight: on String: {'data1'} Tag: 'legend' TextColor: [0 0 0] Title: [1x1 Text] Type: 'legend' Units: 'normalized' UserData: [] Visible: on
figure
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
[h,objh1,objh2]=legend(p);
get(h)
AutoUpdate: off BackgroundAlpha: 1 BeingDeleted: off Box: on BusyAction: 'queue' ButtonDownFcn: @bdowncb Children: [0x0 GraphicsPlaceholder] Color: [1 1 1] ContextMenu: [0x0 GraphicsPlaceholder] CreateFcn: '' DeleteFcn: '' Direction: 'normal' DirectionMode: 'auto' EdgeColor: [0.1500 0.1500 0.1500] FontAngle: 'normal' FontName: 'Helvetica' FontSize: 9 FontWeight: 'normal' HandleVisibility: 'on' HitTest: on Interpreter: 'tex' Interruptible: off ItemHitFcn: @defaultItemHitCallback Layout: [0x0 matlab.ui.layout.LayoutOptions] LineWidth: 0.5000 Location: 'northeast' NumColumns: 1 NumColumnsMode: 'auto' Orientation: 'vertical' Parent: [1x1 Figure] PickableParts: 'visible' Position: [0.7327 0.8568 0.1529 0.0439] Selected: off SelectionHighlight: on String: {'data1'} Tag: 'legend' TextColor: [0 0 0] Title: [1x1 Text] Type: 'legend' Units: 'normalized' UserData: [] Visible: on
get(objh1)
AffectAutoLimits: off BackgroundColor: 'none' BeingDeleted: off BusyAction: 'queue' ButtonDownFcn: '' Children: [0x0 GraphicsPlaceholder] Clipping: off Color: [0 0 0] ColorMode: 'manual' ContextMenu: [0x0 GraphicsPlaceholder] CreateFcn: '' DeleteFcn: '' EdgeColor: 'none' Editing: off Extent: [0.5467 0.1053 0.4091 0.7895] FontAngle: 'normal' FontName: 'Helvetica' FontSize: 9 FontSmoothing: on FontUnits: 'points' FontWeight: 'normal' HandleVisibility: 'on' HitTest: off HorizontalAlignment: 'left' Interactions: [] Interpreter: 'tex' Interruptible: off LineStyle: '-' LineWidth: 0.5000 Margin: 3 Parent: [1x1 Legend] PickableParts: 'visible' Position: [0.5467 0.5000 0] Rotation: 0 Selected: off SelectionHighlight: off SeriesIndex: 'none' String: 'data1' Tag: '' Type: 'text' Units: 'data' UserData: [] VerticalAlignment: 'middle' Visible: on
get(objh2)
AlignVertexCenters: off Annotation: [1x1 matlab.graphics.eventdata.Annotation] BeingDeleted: off BusyAction: 'queue' ButtonDownFcn: '' Children: [0x0 GraphicsPlaceholder] Clipping: on ContextMenu: [0x0 GraphicsPlaceholder] CreateFcn: '' DeleteFcn: '' DisplayName: 'data1' EdgeAlpha: 1 EdgeColor: [0 0 0] FaceAlpha: 0.3500 FaceColor: [0 0.4470 0.7410] FaceColorMode: 'auto' HandleVisibility: 'on' HitTest: on HoleEdgeAlpha: 1 HoleEdgeAlphaMode: 'auto' HoleEdgeColor: [0 0 0] HoleEdgeColorMode: 'auto' Interruptible: on LineJoin: 'round' LineStyle: '-' LineWidth: 0.5000 Parent: [1x1 Axes] PickableParts: 'visible' Selected: off SelectionHighlight: on SeriesIndex: 1 Shape: [1x1 polyshape] Tag: '' Type: 'polygon' UserData: [] Visible: on
.

추가 답변 (1개)

Afiq Azaibi
Afiq Azaibi 2024년 10월 4일
편집: Afiq Azaibi 2024년 10월 7일
Using multiple outputs from the legend command is discouraged. You mentioned how you are trying to change the size of the icons which isn't currently supported but is one we are aware of. This might not be exactly what you are looking for but one way to change the size of the icon starting in R2024b, you can leverage the legend's IconColumnWidth property to adjust the width allocated to the icon of the legend.
p=plot(polyshape([0 0 1 1],[1 0 0 1]));
l=legend(p);
l.IconColumnWidth = 10;
In some cases when all the icons are markers, the value of IconColumnWidth will be adjusted to be narrow automatically but in all other cases, it will have its old default value which can still be adjusted.
scatter(rand(3,10), rand(3,10), 'filled');
l = legend;

카테고리

질문:

Sim
2024년 6월 28일

편집:

2024년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by