Contour label properties are a mess
33 views (last 30 days)
Show older comments
Apparently Matlab is most useless when it comes to plotting more than a single line. The options to manipulate the labeling of contour lines, i.e. in contourf and with the help of clabel, are a mess.
Say you want latex formatting?
[M,c] = contourf(X,Y,u'*1e6, 8, "LabelFormat","%0.0f $^\\circ C$")
clabel(M,c, 'Interpreter', 'latex')
The white space is excessive, because the vertex points of the contour lines are computed BEFORE the label string is formatted by the interpreter! See for yourself:
Who in the world coded this?
Say you want to manipulate the position of labels, e.g. in the plot below?
Well, you can do
clabel(M,c,'manual');
and set the points with clicks, but then the lines will go through them and 'margin' does not affect anything if the 'background color' must be transparent.
You want to generally control the spacing around the label textboxes? You're in bad luck. It turns out there are hidden properties like Textprims and Edgeprims in your contour object. But the whole thing is programmed so badly that the margin that is seen in the first image is a result of the lines STOPPING and then CONTINUING after the text box. They are precalculated! Stored in c.EdgePrims(1).VertexData. Have fun manually correcting the line data.
With the goal of keeping my sanity, I will no longer use Matlab for plotting purposes. Just leaving this here as user feedback.
1 Comment
Benjamin Kraus
on 7 Apr 2023
Thanks for reporting this @Felix Schönig. I've created a bug report for you. Good luck with your thesis.
Accepted Answer
DGM
on 1 Apr 2023
Edited: DGM
on 1 Apr 2023
Well, this isn't really an answer, since it's not really a question, but this may be related workaround in part (the manual mode problem):
I'm not sure if the text object extents will be reliable for objects created with the LaTeX interpreter, but they seem to be close when created manually as in the linked example. You might need to add a bit of extra padding.
x = 1:100;
y = x';
z = x+y;
[c hc] = contour(x,y,z); hold on
clabel(c,hc,'manual')
% find all the text objects and create underlays
gob = findobj(gca,'type','text');
axar = get(gca,'plotboxaspectratio');
for k = 1:numel(gob)
gob(k).Interpreter = 'latex';
gob(k).String = sprintf('%0.0f $^\\circ C$',str2double(gob(k).String));
p = gob(k).Extent;
th = gob(k).Rotation;
R = [cosd(th) -sind(th); sind(th) cosd(th)].*[1; axar(1)];
% create point list, transform to fit
xy = [p(1)+[0 p(3) p(3) 0 0]; p(2)+[0 0 p(4) p(4) 0]].';
boxc = min(xy,[],1)+range(xy,1)/2;
xy = (R*(xy-boxc).').'+ boxc;
% create white patch, move text on top of it
hp = patch(xy(:,1),xy(:,2),'w');
hp.EdgeColor = 'none';
uistack(gob(k),'top')
end
I don't know about anyone else, but I'd call the first example (the incorrect padding calculation) a bug. If your patience isn't exhausted yet, consider filing a bug report.
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!