フィルターのクリア

How to make the contour labels (numbers) use the same color as the contour lines?

30 ビュー (過去 30 日間)
qilin guo
qilin guo 2022 年 3 月 23 日
編集済み: Alexandre 2023 年 9 月 20 日
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.
[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.
Best regards,
Qilin.
  6 件のコメント
qilin guo
qilin guo 2022 年 3 月 24 日
@Adam DanzDon't worry.
qilin guo
qilin guo 2022 年 3 月 24 日
@DGMThank you!

サインインしてコメントする。

採用された回答

Adam Danz
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)))');

その他の回答 (1 件)

Alexandre
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.

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by