The Woes of contourf patches, findobj, and Matlab 2014b.
5 次查看(过去 30 天)
显示 更早的评论
Hello all, thanks in advance for help.
With the recent update to OS X Yosemite, and of course the required update to MATLAB 2014b, AND the graphics changes to 2014b, I am experiencing some problems with our legacy code.
Background: We have a script called cpatch.m (see below), which [before updating] used
findobj(gca,'type','patches');
on a contourf plot to recolor the patches so that they were a specific color corresponding to a specific contour interval (necessary for the accuracy of our science).
Problem: Contourf in 2014b creates an handle which no longer has any children. It used to be the case that hidden in the children, were the individual patches that one could recolor. Now, these patches do not exist, so one cannot recolor them. In MATLAB:
patches = findobj(gca,'type','patch')
returns
0x0 empty GraphicsPlaceholder array.
and hence is useless.
Ideal Solution: Can someone please tell me how to access the individual patches so that I can change their face color?
Code:
clev = 0:.2:1; % These are the contour levels
colors = bone(length(clev)-1);
cpt = [clev(1:end-1)' clev(2:end)'];
[C,H] = contourf(rand(10,10),clev);
patches = findobj(gca,'type','patch');
for ipatches=1:length(patches)
c = get(patches(ipatches),'cdata');
if length(c) <= 1
ii = find(cpt(:,1) <= c);
iwant = max(ii);
if length(iwant) >= 1
set(patches(ipatches),'facecolor',colors(iwant,:));
end
end
end
Please help! Thanks!
0 个评论
回答(1 个)
Kelly Kearney
2015-1-12
Can I recommend contourfcmap? I believe it does exactly what your code used to do (assign precise colors to the shaded intervals in a contourf plot), and it works in R2014b.
If you look under the hood of that function, you can find the workaround to extract and recolor. Pre-2014b, contour objects were composed of patches. In 2014b, their underlying polygon components are TriangleStrips, rather than simple patches, and their properties are mostly hidden from the user. But you can still hack into it.
[X,Y,Z] = peaks;
[l, h] = contourf(X,Y,Z,4);
h.FacePrims(1).ColorData = uint8([255 0 0 255]'); % change first interval to red
12 个评论
Kelly Kearney
2015-3-25
If by blocks of data, do you mean the contour lines seem to zig-zag in pixelated manner based on the underlying data resolution?
[x,y,z] = peaks(100);
dn = now;
clev = -8:8;
c = contourcs(x(1,:)+dn,y(:,1),z);
ax(1) = subplot(2,2,1);
contour(x+dn,y,z,clev);
ax(2) = subplot(2,2,2);
contourf(x+dn,y,z,clev);
ax(3) = subplot(2,2,3);
hold on;
arrayfun(@(X) plot(X.X, X.Y), c);
linkaxes(ax, 'x');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/182470/image.png)
If so, then no, I haven't found a way around that behavior, which seems to be a bug in R2014b contour and contourf. Luckily, it does seem to be resolved in 2015a.
With contourfcmap, using the calccontour method will avoid the bug, since it plots lines using the output of contourc, and the zig-zaggy bug doesn't seem to manifest itself in countourc.
Lauren Johnson
2015-3-27
This zig-zag look is exactly what I am seeing. I will try in 2015a and avoid using 2014b. Thank you for your insight and quick response.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!