Change format of field contour labels that have been manually added in a tiled layout

조회 수: 3 (최근 30일)
Hi,
Trying to manually add contour labels over a filled contour plot over a narrow range of values.
Matlab returns the error:
Error using hgconvertunits
The reference object is invalid.
Any ideas how I can manually add labels to a tiledlayout?
I also assume there is no workaround for avoiding the contours from striking through the labels when added manually?
Thank you
a = 0.98;
b = 1.02;
Z = round(((b-a).*rand(99,99) + a),2);
XY = [-0.375:.125:.375];
no_levels = 8;
npoints = [99 99]; % define improved resolution
Xfine = linspace(XY(1),XY(end),npoints(1)); % y coords
Yfine = linspace(XY(1),XY(end),npoints(2)); % z coords
[Xfine Yfine] = meshgrid(Xfine,Yfine); % create mesh
tiledlayout(1,2)
for i = 1:2
nexttile(1)
[C,H] = contourf(Xfine,Yfine,Z,no_levels,'-');
% clabel(C,H,'interpreter','latex','FontSize',14);
% H.LevelList = round(H.LevelList,1); % rounds levels to 1st decimal place
clabel(C,H,"manual",'FontSize', 22)
hold on
ylim([-0.5 0.5]);
y = ylim;
yticks([y(1):.25:y(2)])
xticks([-.5:.25:.5])
xlim([-.55 .55])
axis square
end

채택된 답변

Mathieu NOE
Mathieu NOE 2024년 9월 10일
why not create explicitely the levels (V array) ?
a = 0.98;
b = 1.02;
Z = peaks(99)/100 + (a+b)/2 ;
XY = [-0.375:.125:.375];
no_levels = 8;
V = round(linspace(min(Z,[],'all'),max(Z,[],'all'),no_levels),2); % create levels explicitely
npoints = [99 99]; % define improved resolution
Xfine = linspace(XY(1),XY(end),npoints(1)); % y coords
Yfine = linspace(XY(1),XY(end),npoints(2)); % z coords
[Xfine, Yfine] = meshgrid(Xfine,Yfine); % create mesh
tiledlayout(1,2)
for i = 1:2
nexttile(1)
[C,H] = contourf(Xfine,Yfine,Z,V);
clabel(C,H,'interpreter','latex','FontSize',10);
hold on
ylim([-0.5 0.5]);
y = ylim;
yticks([y(1):.25:y(2)])
xticks([-.5:.25:.5])
xlim([-.55 .55])
axis square
end
  댓글 수: 1
Daniel
Daniel 2024년 9월 10일
이동: Voss 2024년 9월 10일
Yes, this seems like a reasonable solution. These things are really not satisfying to work with! Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

praguna manvi
praguna manvi 2024년 9월 10일
편집: praguna manvi 2024년 9월 10일
I also observed the same error at “clabel(C, H, “manual”)” inside tiledLayout. I find that you could use subplot for manual labelling as follows to achieve the same effect for a 1 x 2 layout:
a = 0.98;
b = 1.02;
Z = round(((b-a).*rand(99,99) + a),2);
XY = [-0.375:.125:.375];
no_levels = 8;
npoints = [99 99];
Xfine = linspace(XY(1),XY(end),npoints(1));
Yfine = linspace(XY(1),XY(end),npoints(2));
[Xfine, Yfine] = meshgrid(Xfine,Yfine);
figure;
for i = 1:2
subplot(1, 2, i);
[C,H] = contourf(Xfine,Yfine,Z,no_levels,'-');
clabel(C,H,"manual",'FontSize', 22, "Color", [1, 1, 1]);
hold on;
ylim([-0.5 0.5]);
y = ylim;
yticks([y(1):.25:y(2)]);
xticks([-.5:.25:.5]);
xlim([-.55 .55]);
axis square;
end
Hope this helps!
  댓글 수: 1
Daniel
Daniel 2024년 9월 10일
Thank you for your help, Praguna. So many of my plots are in tiled layouts, I actually don't think that it's worth the effort to change everything for a few labels. I'm never content with how they look anyway, manually placed or otherwise. I'll just stick with the colour axis to guide the reader.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by