How to correctly adjust the FaceColor property of patch objects in a figure with a legend?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello community,
I am having an issue adjusting the FaceColor property of patch objects in a figure. That is, only the legend is updated when I try to change the color of a patch object. My issue can be replicated with the code below:
% Load the figure
openfig('cylinder.fig');
The figure contains four patch objects combined to form a cylindrical shape. The figure also contains a legend.
When I save the patch objects to the variable patches I do not receive any errors:
% Get all patch objects in the figure
patches = findobj(gcf,'Type','patch');
disp(['The number of patch objects is ' num2str(length(patches)) '.'])
However, when I try to change the face color of one of the patch objects, the color is only changed inside of the legend. For example,
patches(1).FaceColor = 'r';
results in the following visual change:
My best guess is that the findobj() function is only identifying the patch objects in the legend and not the objects composing the cylinder, but I am not sure how to confirm this.
My question: Am I making a programmatic mistake? Or is there a bug in how MATLAB is applying the color change operation to the patch object? Thank you in advance for the help!
P.S. I am using MATLAB Online for my problem, in case that makes a difference.
댓글 수: 1
Walter Roberson
2024년 9월 11일
Weird.
The axes has 5 children; the first of those is a Rectangle. Changing the properties of the rectangle is visually reflected.
Changing the properties of the other four children (all patches) is not visually reflected -- even if you turn the patch visually off, or even if you set the XData to be all zeros.
채택된 답변
Voss
2024년 9월 11일
fig = openfig('cylinder.fig','visible');
There are actually 12 patches in the figure
all_patches = findall(fig,'Type','patch')
but 8 of them have their HandleVisibility set to 'off', which is why those are not found by findobj
get(all_patches,'HandleVisibility')
4 of the 'off' HandleVisibility patches have empty XData
get(all_patches,'XData')
I'll copy each patch to its own axes to see where they all are
figure('Position',[10 10 300 900])
tiledlayout(4,3)
for ii = 1:12
ax = nexttile();
copyobj(all_patches(ii),ax)
set(ax,'ZDir','reverse','ZLim',[0 180])
view(ax,3)
title(ax,sprintf('patch #%d',ii))
grid(ax,'on')
end
Your syntax for setting the FaceColor is OK, once you make sure you're operating on the patch you want to be operating on.
% change two patches' FaceColor
all_patches(2).FaceColor = 'y';
all_patches(3).FaceColor = 'r';
% copy the 1st 3 again to see how the new colors have been applied
figure
tiledlayout(1,3)
for ii = 1:3
ax = nexttile();
copyobj(all_patches(ii),ax)
set(ax,'ZDir','reverse','ZLim',[0 180])
view(ax,3)
title(ax,sprintf('patch #%d',ii))
grid(ax,'on')
end
Elements 3, 6, 9, and 12 of all_patches are the ones reflected in the legend (that's why the legend updates when they change FaceColor), but they are not contained in the legend, which you can tell by deleting the legend and seeing that findall still returns the same set of patches.
figure(fig)
delete(gca().Legend)
new_all_patches = findall(fig,'Type','patch');
isequal(new_all_patches,all_patches)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!