【问题标题】:Matlab legend color incorrect, in a loop of variable size, reading in legend namesMatlab图例颜色不正确,在可变大小的循环中,读取图例名称
【发布时间】:2013-10-17 09:37:08
【问题描述】:

我觉得我已经阅读/尝试了所有关于此的在线建议 - 可能错过了明显的!在 Matlab R2013a 中,我从一个循环中绘制多条线(每次读取的行数是可变的),然后添加一个图例,其中系列名称是从列表中读取的。图例上的线条颜色与图上的线条颜色不匹配。

我已经尝试过彩色地图、获取和设置命令、将图例放入循环中和循环之外、为图例创建单独的循环等...请帮助!相关部分代码如下:

%% getting the data....
for i=1:length(files); % for each file in the folder
  FileName = files(i,1).name; % extract the filename
  calfile = files(i,1).name; % lists all filenames for metadata
  a = length(calfile);
  fn(i,1:a) = calfile;
  fid_data(i) = fopen(files(i,1).name,'r'); % open that file
  data = csvread(FileName,0,1,[0 1 15 30]); % 
  diam(i,:) = data(9,:); % puts each channel center diameter into array
  diamerr(i,:) = data(10,:); % channel centre error
end

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

%% Plotting data
h = (figure('name','campaign summary','numbertitle','off'));
set(gca, 'box','on')

for r = 1:i
   plot(x,diam(r,:)); % plots diameters for each flight
   hold all
   errorbar(diam(r,:), diamerr(r,:)); % plots error bars for each flight diameters
end

legend(fn(:,:),'location', 'northwest'); % legend derived from filename list
set(legend,'Interpreter','none'); % stops the underscore from making text subscript

【问题讨论】:

  • 你能提供一个最小的例子吗?

标签: matlab loops colors legend


【解决方案1】:

您不需要同时使用 ploterrorbarerrorbar 函数在循环前面用plot 绘制的线上绘制。然后将图例应用于这些隐藏线以及可见线。摆脱plot 并改用errorbar(x, diam(r,:), diamerr(r,:))

【讨论】:

  • 太棒了!这是一种享受,非常感谢你的帮助。压力水平现在正在消退..... :0)
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 2017-05-01
  • 2014-05-18
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多