如何在 MATLAB 中用颜色条替换图例?

问题描述 投票:0回答:1

我有一个包含多条线的线图,但我更喜欢有一个颜色条来指示每条线的值(在我的情况下为时间),而不是图例。

到目前为止我最好的尝试如下(我只包含代码的相关部分):

colormap('jet');
cmin = min(t);
cmax = max(t);
cmp1 = jet(7); % say, there are 7 lines in my plot 
colororder(cmp1); 

plot() % plot without including 'Color' argument 

colorbar('Location', 'eastoutside');
clim([cmin cmax]);

我在这里面临的问题是线条的颜色与颜色条不完全对应。

参见 this plot 示例。您可以清楚地看到颜色与颜色栏中的值不完全匹配(图例具有正确的值)。

matlab plot line matlab-figure colorbar
1个回答
0
投票

这对你有用吗?

colormap('jet');
t = [1, 600, 1200, 1800, 2400, 3000, 3600];
cmin = min(t);
cmax = max(t);
cmp1 = jet(7); % say, there are 7 lines in my plot 
colororder(cmp1); 

plot(magic(7), 'linewidth', 3) % plot without including 'Color' argument 

cbar = colorbar('Location', 'eastoutside');
clim([cmin, cmax])
set(cbar, 'Ticks', t)

看看第二个是否适合您。我不完全明白你的意图。

colormap('jet');
t = [1, 600, 1200, 1800, 2400, 3000, 3600];
cmin = min(t);
cmax = max(t);
cmp1 = jet(7); % say, there are 7 lines in my plot 
colororder(cmp1); 

plot(magic(7), 'linewidth', 3) % plot without including 'Color' argument 

cbar = colorbar('Location', 'eastoutside');
colormap(cmp1)
temp = movmean(linspace(0, 1, 7 + 1), 2);
set(cbar, 'Ticks', temp(2:end))
set(cbar, 'TickLabels', {'foo','bar','ha','ho','hi','hu'})

© www.soinside.com 2019 - 2024. All rights reserved.