Name Contour levels outside plot
Mostrar comentarios más antiguos
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,
Marvin
1 comentario
dpb
el 16 de Nov. de 2021
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 point...it's unlikely somebody will take the time to try to recreate something from scratch...
Respuesta aceptada
Más respuestas (2)
Kelly Kearney
el 16 de Nov. de 2021
1 voto
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 comentarios
Star Strider
el 16 de Nov. de 2021
.
Kelly Kearney
el 17 de Nov. de 2021
Definitely not trivial. My contourfcmap code does something similar under the hood (in the extend2wall subfunction) because it needs to know where every contour line intersects the axis box in order to redraw new patches... but I use a number of external FEX functions to do the heavy lifting there.
Marvin Gerhardt
el 22 de Nov. de 2021
The following demo
- produces a contourf plot
- extracts the coordinates of the contour lines
- determines which lines intersect with the right axis edge
- plots text labels at the intersections (if any)
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)
colorbar(ax)

Compare to a labeled contour version,

1 comentario
Marvin Gerhardt
el 22 de Nov. de 2021
Categorías
Más información sobre Contour Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
