필터 지우기
필터 지우기

Labels on the contour have too many digits

조회 수: 46 (최근 30일)
Maryam
Maryam 2013년 7월 29일
답변: mauricio misraji 2023년 3월 15일
How can I set the precision of the numbers on my contour plot? Now the clabels are like: 1.29567, I would like to have it rounded like: 1.3.
Thanks

채택된 답변

Maryam
Maryam 2013년 7월 30일
I have done the following and it works perfectly:
[c,h]=contourf(X,Y,H); texth=clabel(c,h,'manual','fontsize', 20); for i=1:size(texth) textstr=get(texth(i),'String'); textnum=STR2DOUBLE(textstr); textstrnew=sprintf('%0.3f', textnum) set(texth(i),'String',textstrnew); end
  댓글 수: 1
Johannes S.
Johannes S. 2022년 2월 22일
Thank you so much!! I was searching for something like this for about two days now.
Its a german thing, that you need to use a comma for decimal sep. instead of a dot and this is the best solution for the contour levels.

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

추가 답변 (4개)

Matthew Perkins
Matthew Perkins 2018년 9월 10일
I found this works. And seems much easier. I have R2015b.
[c,h]=contourf(X,Y,H);
h.LevelList=round(h.LevelList,3) %rounds levels to 3rd decimal place
clabel(c,h)
  댓글 수: 3
MohammadReza Jabbari (Jabari)
MohammadReza Jabbari (Jabari) 2020년 7월 16일
this is worked. thanks
Zhao Shuaijie
Zhao Shuaijie 2021년 2월 17일
works well !
thanks

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


Moises Jezzini
Moises Jezzini 2016년 9월 12일
편집: Moises Jezzini 2016년 9월 12일
An update since this was the first answer in my search. This works for 2014b or above, previous solution does not work, due to a change in the way clabel works.
[X,Y,Z] = peaks;
figure;
[C, ~] = contour(X, Y, Z, 5, 'ShowText', 'on');
tl = clabel(C, 'FontSize', 10);
itvec = 2:2:length(tl);
NewCoutours = zeros(size(itvec));
for i= itvec
textstr = get(tl(i), 'String');
NewCoutours(i) = round(str2double(textstr), 2);
end
contour(X,Y,Z, NewCoutours, 'ShowText','on');
Before
After

Hugo
Hugo 2013년 7월 30일
You could use
[C,h]=contour(...);
texth=clabel(C,h);
texth is a vector that contains the handles of all labels. To get the value in label number n you can use
textstr=get(texth(n),'String');
textstr is a string of chars, not a number, so you should do
textnum=str2num(textstr);
to convert to a number. Then you can set the precision by using
textstrnew=num2str(textstr,'%1.1g');
and then set the label to
set(texth(n),'String',teststrnew);
Hope this helps.
  댓글 수: 2
Maryam
Maryam 2013년 7월 30일
Thank you very much for your help. It does not work. This is what I have:
[c,h]=contourf(Xe,Ye,Z,30); texth=clabel(c,h,'fontsize', 20); for i=1:size(texth) textstr=get(texth(i),'String'); textnum=str2num(textstr); textstrnew=num2str(textstr,1); set(texth(i),'String',textstrnew); end
I appreciate your help. Thanks.
Hugo
Hugo 2013년 8월 2일
It doesn't work because you wrote "textstrnew=num2str(textstr,1);" instead of what I wrote. The second input argument should be an appropriate one.

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


mauricio misraji
mauricio misraji 2023년 3월 15일
You can add the option to the contour command:
"LabelFormat","%0.1f"
where 0.1 means just 1 decimal digit. For example:
contour(X,Y,Z,levels,'ShowText','on',"LabelFormat","%0.4f")
shows 4 decimal digits.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by