Contour label properties are a mess

33 visualizaciones (últimos 30 días)
Felix Schönig
Felix Schönig el 1 de Abr. de 2023
Comentada: Benjamin Kraus el 7 de Abr. de 2023
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 comentario
Benjamin Kraus
Benjamin Kraus el 7 de Abr. de 2023
Thanks for reporting this @Felix Schönig. I've created a bug report for you. Good luck with your thesis.

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 1 de Abr. de 2023
Editada: DGM el 1 de Abr. de 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.
  1 comentario
Felix Schönig
Felix Schönig el 1 de Abr. de 2023
Thanks for your suggestion. Had to blow off some steam, please apologize my tone. I'll file a bug report once I have handed in my thesis. For the moment, my patience is exhausted. I'll accept your answer, there is nothing better.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by