Name Contour levels outside plot

조회 수: 6 (최근 30일)
Marvin Gerhardt
Marvin Gerhardt . 2021년 11월 16일
댓글: Star Strider . 2021년 11월 22일
Dear Community,
i hope that this is not a duplicate, i did research for some weeks now and havent found a solution:
Is there a way to place the text of a contour line outside on the right y-axis? See Picture below:
Backround is that ive got an efficiency map with speed(x), torque(y) and efficiency(z) and wanted to place power as isolines over the contourf plot and put the values of the isolines outside on the right y-axis.
Thanks a lot in advance,
  댓글 수: 1
dpb 2021년 11월 16일
As usually the case, if you would like somebody to try to explore modifications to your plot, attach the data and code to let them generate the starting's unlikely somebody will take the time to try to recreate something from scratch...

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

채택된 답변

Star Strider
Star Strider 2021년 11월 16일
It took longer than expected for me to get this to work.
NOTE — It only works for contours that intersect the y-axis on the right. Plotting peaks completely confuses it, so it gives the wrong result, however it does not throw any errors.
x = 0:0.01:1;
y = tanh(x)+0.05;
z = x(:)*y;
[c,h] = contourf(x, y, z, 'ShowText','off');
hold on
Lvls = h.LevelList;
idx = ones(size(Lvls));
xlv = xlim;
for k1 = 1:numel(Lvls)
lc = find( (c(1,:)==Lvls(k1)) & (mod(c(2,:),1)==0) );
seglen = c(2,lc);
seglenix = seglen>1;
if any(seglenix)
for k2 = 1:numel(seglen(seglenix))
lciv = lc(k2)+(1:seglen(k2)); % Level Contour Index Vector
lcvm = [c(1,lciv); c(2,lciv)]; % Level Contour Matrix
% lcvc{k1,k2} = [c(1,lciv); c(2,lciv)]; % Level Contour Cell Array
[maxx,idxx] = max(lcvm(1,:));
% plot(lcvm(1,idxx), lcvm(2,idxx), 'p')
ylbl(:,k1) = [Lvls(k1); lcvm(1,idxx); lcvm(2,idxx)]; % Y-Label Matrix
% fprintf('\n------------------------------------------------------------\n')
% display_ylabel_matrix = ylbl
hold off
text(ones(size(ylbl(1,:)))*max(xlim), ylbl(3,:), compose(' %0.2f',ylbl(3,:)), 'Horiz','left', 'Vert','middle')
I tested it with a number of functions that meet the criteria it requires, and it appears to be reasonably robust.
  댓글 수: 3
Star Strider
Star Strider 2021년 11월 22일
As always, my pleasure!

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

추가 답변 (2개)

Kelly Kearney
Kelly Kearney 2021년 11월 16일
Using builtin contour-related functions... no, not really. Contour labels are one of the least customizable parts of Matlab graphics. In order to do this, I'd recommend manually calculating where each contour is expected to intersect the right side of your plot, and then adding text objects.
  댓글 수: 3
Marvin Gerhardt
Marvin Gerhardt 2021년 11월 22일
Thanks !

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

Adam Danz
Adam Danz 2021년 11월 16일
편집: Adam Danz 님. 2021년 11월 16일
The following demo
  1. produces a contourf plot
  2. extracts the coordinates of the contour lines
  3. determines which lines intersect with the right axis edge
  4. plots text labels at the intersections (if any)
Step 2 is achieved by using getContourLineCoordinates from the file exchange.
Note that this works for all contours I tested, even those that do not intersect the right axis (their lables will not be included).
% Plot contourf
x = 0:.1:1;
y = x;
z = x(:).*y(:)';
ax = gca();
cm = contourf(ax, x, y, z);
% Get contour line coordinates
contourTable = getContourLineCoordinates(cm);
% Get index of lines that intersect with the right edge of the axes
% axis(ax, 'tight') % if needed
intersectsWithY = contourTable.X == ax.XLim(2);
% Remove duplicate values
edgeCoordinates = [contourTable.X(intersectsWithY), contourTable.Y(intersectsWithY), contourTable.Level(intersectsWithY)];
edgeCoordUnq = unique(edgeCoordinates,'rows');
% Add text indicating level values on the outside edge of the right axis
hold(ax, 'on')
xlim(ax, xlim(ax))
ylim(ax, ylim(ax))
text(edgeCoordUnq(:,1), edgeCoordUnq(:,2), compose('%.2g', edgeCoordUnq(:,3)), ...
'HorizontalAlignment', 'Left', 'VerticalAlignment','middle', ...
'FontSize', 8)
Compare to a labeled contour version,
  댓글 수: 1
Marvin Gerhardt
Marvin Gerhardt 2021년 11월 22일
Thanks Adam !

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


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