gpt4 book ai didi

matlab - 如何为 MATLAB 绘图中的刻度标签分配多种颜色?

转载 作者:太空宇宙 更新时间:2023-11-03 19:37:56 24 4
gpt4 key购买 nike

是否可以在 MATLAB 中的一个轴上为单个数字(或一组数字)着色?

假设我有一个情节:

plot(1:10, rand(1,10))

现在,我可以例如使 x 轴上的数字 3 变成红色?

最佳答案

单刻度标签可以使用 tex markup 着色,默认情况下为刻度标签启用。它在 TickLabelInterpreter 中定义轴的属性。

它提供了两个着色文本的命令:

  • \color{<name>} , 其中<name>是颜色名称,例如“红色”或“绿色”,并且
  • \color[rgb]{<R>,<G>,<B>} , 其中<R> , <G><B>是介于 0 和 1 之间的数字,并定义 RGB 颜色。

这些命令可用于为单刻度标签着色:

plot(1:10, rand(1,10))
ax = gca;

% Simply color an XTickLabel
ax.XTickLabel{3} = ['\color{red}' ax.XTickLabel{3}];

% Use TeX symbols
ax.XTickLabel{4} = '\color{blue} \uparrow';

% Use multiple colors in one XTickLabel
ax.XTickLabel{5} = '\color[rgb]{0,1,0}green\color{orange}?';

% Color YTickLabels with colormap
nColors = numel(ax.YTickLabel);
cm = jet(nColors);
for i = 1:nColors
ax.YTickLabel{i} = sprintf('\\color[rgb]{%f,%f,%f}%s', ...
cm(i,:), ax.YTickLabel{i});
end

结果是这样的:

result

代码在 MATLAB R2016b 和 R2017a 中对我有用。

关于matlab - 如何为 MATLAB 绘图中的刻度标签分配多种颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16676668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com