Clear Filters
Clear Filters

How to control the font of Contour "ShowText" option?

90 views (last 30 days)
I am generating 2-D contour filled plot using the function "contourf". Further I specify levels and mark the levels with "ShowText",'on', Option. My question is how to control the size and appearance of the font. Thanks in advance.
  1 Comment
Pappu Murthy
Pappu Murthy on 22 Jun 2023
NP's suggested solution is exactly what I needed and it worked fine. DGM's approach is more elaborate and complex while it works, it is not necessary for my specific purpose.
Thanks all for pitching in.
Pappu Murthy

Sign in to comment.

Accepted Answer

Nitya Patel
Nitya Patel on 22 Jun 2023
Edited: Nitya Patel on 22 Jun 2023
It is my understanding that you want to change the font size and appearance of the labels in a contour plot. You can check out how-can-i-change-the-font-and-the-size-of-labels-on-contours.
They are using another way to label the contour or contourf plots, called clabel.
You can use it in the following way:
% Any data
[x,y,z] = peaks;
% Plot the contour and store the values of Contour Matrix and Object
[C,h] = contourf(x,y,z);
% Use the matrix and object to add labels, you can specify the formatting
% options
clabel(C,h, 'FontSize', 15, 'FontWeight','bold', 'Color','blue');
Documentation Link: clabel
  1 Comment
DGM
DGM on 22 Jun 2023
Edited: DGM on 22 Jun 2023
If all labels are to have the same properties, this is probably the best approach. For some reason, I was thinking that the question was about manipulating labels individually.

Sign in to comment.

More Answers (2)

DGM
DGM on 22 Jun 2023
Edited: DGM on 22 Jun 2023
If I recall off the top of my head, the level labels created in 'auto' mode are different than those created in 'manual' mode (i.e. with clabel()). In the latter case, the labels are actual text() objects. In the mode that you describe, the labels are actually undocumented private properties of the contour object. If your goal is to manipulate individual labels, there is no intended way to do it.
That said, you can try some things.
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
[c,hc] = contourf(X,Y,Z,'ShowText','on');
% get handles for text primitives
drawnow
ht = hc.TextPrims;
% the basic properties of a textprim object are:
% Name, Size, Angle, Weight
for k = 1:numel(ht)
ht(k).Font.Weight = 'normal'; % 'normal' or 'bold' only
ht(k).Font.Name = 'courier';
ht(k).Font.Size = 9; % see notes
end
% set one label really big and one really small
ht(7).Font.Size = 28;
ht(8).Font.Size = 5;
Note that a few properties can be set within limits, but size becomes an issue. The gaps in the contour lines for each label are fixed and independent of the textprim object properties. If you make the text significantly larger or smaller, they may become a problem.
How are these gaps set? The contour object also has edgeprim objects which create the contour lines themselves.
he = hc.EdgePrims; % these are the edge primitives
he(4).VertexData % this is the vertex data for this contour line [x; y; z]
he(4).StripData % this is a uint32 vector of indices into the VertexData array
% two ways to change gaps
% to make a gap wider, you might change which segments are hidden
he(4).StripData = uint32([1 10:15 22 33 38]);
% but you can't make a gap smaller the same way, since they're only one segment wide
% you can alternatively try to move vertices or add vertices
% neither option will be simple
he(4).VertexData(:,21:22) = [-0.55 -0.35; 1.17 1.05; 0 0];
Note now that I didn't run this in the forum editor, and I'm posting actual screenshots. The problem with this example is twofold. First, the selection of each particular contour line and vertex varies with figure size and other particulars. This makes working in the forum editor difficult, and it means that if you run this example code, the results won't be the same. That's probably expected, but the bigger problem is that all of these edits to private objects are volatile. If you try to zoom or pan in the figure or force any redraw, they'll all be reset. This also means that you can't use canonical methods like saveas() or print() to save the figure. That severely limits the practical usefulness of any of these workarounds.
Let's say you were willing to use clabel() to create manual labels instead. The labels would then be regular text objects and would be updated correctly. Some properties could be set directly, but others may still require extra work. See the following examples.
FWIW, all properties of an object (including the hidden ones) can be seen by casting it as a struct. It's a mess to wade through, and a lot of things might have unintended consequences when edited.
struct(hc)

Vishnu
Vishnu on 22 Jun 2023
Hi Pappu Murty,
To control the size and appearance of the font in the "contourf" plot, you can use the "TextList" property of the plot object. This property allows you to access and modify the properties of the text labels for each contour level. Specifically, you can modify the 'FontSize', 'FontWeight', 'FontAngle', and 'FontName' properties of each text label to control the size and appearance of the font.
Here is an example code:
Generate contourf plot with three levels
levels = [-0.6 -0.3 0.15];
contourf(X,Y,Z,levels)
Add level labels with custom font properties
hct = contours(X,Y,Z,levels);
clabel(hct,'LabelSpacing',1000,'Color','k','FontWeight','bold','FontSize',30);
textObjs = findobj(hct,'Type','text');
set(textObjs,'FontName','Courier New','FontWeight','bold','FontSize',50);
Hope this will help you.

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!