Contour label properties are a mess
조회 수: 40 (최근 30일)
이전 댓글 표시
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
Benjamin Kraus
2023년 4월 7일
Thanks for reporting this @Felix Schönig. I've created a bug report for you. Good luck with your thesis.
채택된 답변
DGM
2023년 4월 1일
편집: DGM
2023년 4월 1일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!