How to make the contour labels (numbers) use the same color as the contour lines?
29 次查看(过去 30 天)
显示 更早的评论
Dear MATLAB users,
I want to know if it is possible to make clabel follow the color of the contours? Meaning that the numbers have the same color as the contour curves.
Here is an example which could be produced using MATLAB, and I am trying to reproduce it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/938764/image.png)
[This figure is taken from: Phase coherence in graphene by Mark Brian Lundeberg]
This is my MATLAB codes.
% Phase coherence in graphene 石墨烯中的相位相干.pdf, Page 9
clearvars; clc; close all; fclose all; format short; format compact;
a = 1;
t = 1;
kx = linspace(-4*pi/(sqrt(3)*a), 4*pi/(sqrt(3)*a), 101);
[KX, KY] = meshgrid(kx, kx);
energy = t * sqrt(3 + 2*cos(KY*a) + 4 * cos((sqrt(3)/2)*KX*a) .* cos((1/2)*KY*a));
fig = figure('Unit', 'Centimeter', 'Position', [1, 1, 12, 12]);
% ax = axes(fig, 'Unit', 'Centimeter', 'Position', [2, 2, 8, 8]);
ax = axes(fig);
hold on;
[C, h] = contour(ax, KX, KY, energy, 'LineWidth', 0.5);
% https://ww2.mathworks.cn/matlabcentral/answers/83483-labels-on-the-contour-have-too-many-digits
h.LevelList = round(h.LevelList, 3);
clabel(C, h, 'FontSize', 8, 'FontName', 'Arial');
h.LevelList = 0.30:0.20:3.00;
colormap(jet)
% Draw a regular hexagon (e.g., Brillouin zone)
theta = (30:60:360)*pi/180;
vert = (4*pi/(3*a)) * [cos(theta); sin(theta)]; % vertices of hexagon
pgon = polyshape(vert');
plot(pgon);
axis equal;
% saveas(fig, 'TBA_bands_hexagonal_contour', 'pdf');
This is the result.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/938769/image.png)
Best regards,
Qilin.
采纳的回答
Adam Danz
2022-3-26
编辑:Adam Danz
2022-3-26
Generate contour with level labels
Adapted from OP's code:
a = 1;
t = 1;
kx = linspace(-4*pi/(sqrt(3)*a), 4*pi/(sqrt(3)*a), 101);
[KX, KY] = meshgrid(kx, kx);
energy = t * sqrt(3 + 2*cos(KY*a) + 4 * cos((sqrt(3)/2)*KX*a) .* cos((1/2)*KY*a));
fig = figure();
ax = axes(fig);
hold on;
[C, h] = contour(ax, KX, KY, energy, 'LineWidth', 0.5);
h.LevelList = 0.30:0.20:3.00;
clabel(C, h, 'FontSize', 8, 'FontName', 'Arial');
colormap(jet)
Set contour label colors to contour colors
- h is the contour handle from [~,h]=contour(__)
- ax is the axes handle
drawnow() % required
ax.CLim = [min(h.LevelList), max(h.LevelList)];
clabelVals = str2double(get(h.TextPrims, 'string'));
levelNorm = (clabelVals-ax.CLim(1))./(ax.CLim(2)-ax.CLim(1));
colorIdx = round(levelNorm * (size(ax.Colormap,1) - 1))+1;
clabelRGB = uint8([ax.Colormap(colorIdx,:) * 255, zeros(numel(colorIdx),1)])';
set(h.TextPrims, {'ColorData'}, mat2cell(clabelRGB,4,ones(1,size(clabelRGB,2)))');
0 个评论
更多回答(1 个)
Alexandre
2023-9-20
编辑:Alexandre
2023-9-20
As of MATLAB R2023b we have introduced a new Property Input for LabelColor on Contour called 'flat', which will map each Contour Label's Color to the colormap. This color mapping will be equivalent to the Line color mapping, as such, each Contour Label Color will match its associated Contour Line Color.
Please look at our updated documentation for more information:
Updated OP Code:
a = 1;
t = 1;
kx = linspace(-4*pi/(sqrt(3)*a), 4*pi/(sqrt(3)*a), 101);
[KX, KY] = meshgrid(kx, kx);
energy = t * sqrt(3 + 2*cos(KY*a) + 4 * cos((sqrt(3)/2)*KX*a) .* cos((1/2)*KY*a));
fig = figure();
ax = axes(fig);
[C, h] = contour(ax, KX, KY, energy, 'LabelColor', 'flat', 'LineWidth', 0.5);
h.LevelList = 0.30:0.20:3.00;
clabel(C, h, 'FontSize', 8, 'FontName', 'Arial');
colormap jet
Simple Example:
contour(peaks, 'LabelColor', 'flat')
Please look at Adam Danz's answer for how to workaround this limitation in previous releases.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!