Labels on the contour have too many digits
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (4)
Matthew Perkins
el 10 de Sept. de 2018
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 comentarios
Tom Shenton
el 21 de Jun. de 2020
Great elegant solution. Worked really well.
MohammadReza Jabbari (Jabari)
el 16 de Jul. de 2020
this is worked. thanks
Zhao Shuaijie
el 17 de Feb. de 2021
works well !
thanks
Moises Jezzini
el 12 de Sept. de 2016
Editada: Moises Jezzini
el 12 de Sept. de 2016
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
el 30 de Jul. de 2013
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.
mauricio misraji
el 15 de Mzo. de 2023
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.
Categorías
Más información sobre Contour Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!