I would like to find the facecolor of the the contourf patches. For example if :
[X,Y,Z] = peaks;
figure
contourf(X,Y,Z,4)
I would like to get the 5 different facecolors of the patches of the contour.
Thank you

답변 (1개)

Doug Hull
Doug Hull 2014년 3월 11일

0 개 추천

[X,Y,Z] = peaks;
[l, h] = contourf(X,Y,Z,4)
hp = get(h,'children')
get(hp,'cdata')
colorbar
This gets you the value of the cdata. Look at the colormap to get the actual RGB color. Look at the colorbar to see what those values correspond to.

댓글 수: 5

Thank you Doug! But is there any way to get the color codes of those colors? So,get a matrix 5x3?
I wrote a function that extracts this sort of data from a colormap: interpcolor
[X,Y,Z] = peaks;
[l, h] = contourf(X,Y,Z,4);
hp = get(h,'children');
colorbar;
clim = get(gca, 'clim');
cdata = cell2mat(get(hp, 'cdata'));
cmap = colormap;
col = interpcolor(cdata, cmap, clim)
---
col =
1 0.48437 0
0.5 0 0
0 0 0.5625
0 0.54688 1
0 0.54688 1
1 0.48437 0
0.53125 1 0.46875
0 0.54688 1
0 0 0.5625
Kellen
Kellen 2015년 1월 12일
Any ideas on how to do this in 2014b, in which the cdata field no longer exists?
On the plus side, the new version stores the exact color value, so you don't need to use the color interpolation trick. On the down side, the properties are buried under a few layers of undocumented properties, so this may change down the road:
[X,Y,Z] = peaks;
[l, h] = contourf(X,Y,Z,4);
rgba = double(cat(2, h.FacePrims.ColorData))./255;
col = rgba(1:3,:)';
Johannes Korsawe
Johannes Korsawe 2015년 1월 26일
Kelly, is there some undocumented chance for some magic to set some FaceAlpha-Values for the plotted areas of the contourf-command?

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

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

질문:

2014년 3월 10일

댓글:

2015년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by