我在Matlab图中需要不同的颜色 [英] I need different colors in my matlab plot

查看:70
本文介绍了我在Matlab图中需要不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情节代码.问题是我的绘图中的两条线具有相同的颜色,我需要为绘图中的每条线使用一种特殊的颜色(总共4条线).

This is my plot code. The problem is that two lines in my plot have same colors, I need one special for each line in the plot (totally 4 lines).

for i=1:nFolderContents;
    [~, data] = hdrload(folderContents(i,:));
    if size(folderContents(i,:),2)<size(folderContents,2);
        temp=folderContents(i,6:9);
    else
       temp=folderContents(i,6:7);
    end
    temp1(i)=strread(temp);
    w=2*pi*(data([35 51 68 101],1));
    permfreespace=8.854e-12;
    perm=data([36 52 69 101],3);
    cond=perm.*w.*permfreespace;
    conds([36 52 69 101],i)=cond;
    hold on

end


figure(4);plot(temp1,conds);
gcf=figure(4);
set(gcf,'Position', [0 0 295 245]);
xlabel('Temperature [\circC]'), ylabel ('Conductivity [s/m]');
title('Different frequencies');
legend('1.02 GHz','1.50 GHz','2.01 GHz','3 GHz');
axis([20 52 0 4]);
box on 

新代码:

conds=zeros(101,28);
for i=1:nFolderContents;
    [~, data] = hdrload(folderContents(i,:));
    if size(folderContents(i,:),2)<size(folderContents,2);
        temp=folderContents(i,6:9);
  else
   temp=folderContents(i,6:7);
    end
    temp1(i)=strread(temp);
    w=2*pi*(data([35 51 68 101],1));
    permfreespace=8.854e-12;
    perm=data([36 52 69 101],3);
    cond=perm.*w.*permfreespace;
    conds([36 52 69 101],i)=cond;
    hold all

end
diff = hsv(101); 
for i=1:101
    figure(4),plot(temp1(1,:),conds(i,:),'color',diff(i,:));
    hold all;
end
gcf=figure(4);
set(gcf,'Position', [0 0 295 245]);
xlabel('Temperature [\circC]'), ylabel ('Conductivity [s/m]');
title('Different frequencies');
legend('1.02 GHz','1.50 GHz','2.01 GHz','3 GHz');
axis([20 52 0 4]);
box on 

问题是我现在在图例框中得到相同的颜色.

the problem is that I get same color in the legend box now.

推荐答案

您用101行(零位)定义了变量conds,然后将4行更改为某些值.现在,您只想绘制这4条线,但是循环运行101次,因此它也绘制了零线.这就是您得到零行(实际上是97行...)的原因.这也是您在4条曲线上获得相同颜色(可能是各种图形颜色)在零线上浪费"的原因.

You defined the variable conds with 101 lines (of zeros), then changed 4 lines to some values. Now you want plotting only those 4 lines, but your loop runs 101 times, so it plots also the zero-lines. This is the reason you get a line at zero (actually 97 lines...). This is also the reason that you get same colors for the 4 curves, probably the variety of graph colors, "wasted" on the zero lines.

您只能使用

rows=[36 52 69 101] ;
color='rgbc'
for i=1:4
   plot (temp(1,:), cond(rows(i),:), 'color',color(i));
   hold on
end 
hold off

实际上,您根本不需要此conds=zeros(101,28),只需将将conds的值插入的行更正为:

Actually, you don't need this conds=zeros(101,28) at all, just correct the line that insert the values to conds to:

conds=cond([36 52 69 101],:);

而且,我认为您不需要在第一个循环中使用它.

And, I don't think you need it inside the first loop.

这篇关于我在Matlab图中需要不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆