2016-04-14 145 views
1

我搜索了四周,但还没有找到解决方案。我的目标是用三组数据绘制散点图,每组数据都有不同的颜色。下面是我的代码示例:用双y轴更改散点图的颜色

%generate input 
x1=[732490 732509 732512 732513 732521 732528]; 
y1=[7.828 7.609 22.422 14.758 26.258 1.477]; 
x2=[732402 732403 732404 732404 732433 732555]; 
y2=[0.693 0.645 0.668 0.669 0.668 0.662]; 
x3=[832402 832403 832404 832404 832433 835423]; 
y3=[0.693 0.645 0.668 0.669 0.668 0.685]; 
figure(1); 
[ax,h1,h2]=plotyy(x1,y1,[x2,x3],[y2,y3],'scatter'); 
blue=[0 0 1]; 
red=[1 0 0]; 
set(h1,'cdata',red); 
set(h2,'cdata',blue); 
set(ax(1),'ycolor','r'); 
set(ax(2),'ycolor','b'); 

然而,这正是我想要的到底是什么,为[X2 Y2] [X3 Y3]具有相同的颜色。有没有办法改变颜色,使三组数据有不同的颜色?还有如何添加一个显示三组数据的图例?

+1

请问您是否可以纠正代码中的任何错误,它不会像发布一样工作。它说在plotyy> fevalfun错误(行361) – VMMF

+0

对不起,它现在更新和工作。 – James

回答

2

您可以通过建立自己的plotyy克服plotyy限制:

  • 创建一个figure
  • 添加axes
  • 添加第二个axes和:
    • 使其position等于第一轴的一个
    • mak È它透明通过设置其color到`无”

现在可以选择在其上通过指定它作为第一个参数(例如使用scatter轴。 G。 scatter(axes_1, ...))。

绘制完所有数据集后,必须使两个轴的xlim相等。

要添加图例,您只需指定handles" returned by the分散function as first argument of the图例函数。

该方法已在以下代码中实现。

在代码中进行检查以验证是否可以使用dot notation (introduced in R2014b)

x1=[732490 732509 732512 732513 732521 732528]; 
y1=[7.828 7.609 22.422 14.758 26.258 1.477]; 
x2=[732402 732403 732404 732404 732433 732555]; 
y2=[0.693 0.645 0.668 0.669 0.668 0.662]; 
x3=[832402 832403 832404 832404 832433 835423]; 
y3=[0.693 0.645 0.668 0.669 0.668 0.685]; 
% Check if the "dot notation" can be used 
dot_notation=~verLessThan('matlab','8.4') 
% 
figure 
% Add the first axes 
ax1=axes 
% Add the second axes 
ax2=axes 
% Plot the scatter of the first set of data on the first axes 
h1=scatter(ax1,x1,y1,'r','filled') 
% Plot the scatter of the second set of data on the second axes 
h2=scatter(ax2,x2,y2,'b','filled') 
hold on 
% Plot the scatter of the third set of data on the second axes 
h3=scatter(ax2,x3,y3,'g','filled') 
if(dot_notation) 
    % Superimpose the second axes over the first ome 
    ax2.Position=ax1.Position 
    % Make it transparent 
    ax2.Color='none' 
    % Move the YAxis to the right 
    ax2.YAxisLocation='right' 
    % Adjust the X limits 
    x_lim=[min([ax1.XLim ax2.XLim]) max([ax1.XLim ax2.XLim])] 
    ax1.XLim=x_lim 
    ax2.XLim=x_lim 
    % Remove XAxis Tick 
    ax2.XTick=[] 
else 
    ax1_pos=get(ax1,'position'); 
    % Superimpose the second axes over the first ome 
    set(ax2,'Position',ax1_pos) 
    % Make it transparent 
    set(ax2,'color','none') 
    % Move the YAxis to the right 
    set(ax2,'YAxisLocation','right') 
    % Adjust the X limits 
    ax1_x_lim=get(ax1,'xLim'); 
    ax2_x_lim=get(ax2,'xLim'); 
    x_lim=[min([ax1_x_lim ax2_x_lim]) max([ax1_x_lim ax2_x_lim])] 
    set(ax1,'XLim',x_lim) 
    set(ax2,'XLim',x_lim) 
end 
% Add the legend 
[a,b,c,d]=legend(ax2,[h1,h2,h3],'1° data set','2° Data set','3° Data set') 
if(dot_notation) 
    a.Color='none' 
else 
    set(a,'color','w') 
end 
grid(ax1,'on') 

enter image description here

希望这有助于。

Qapla'

+0

感谢您的详细步骤。我能够将这种方法应用于我的大型数据集,并且按预期工作。我改变的唯一的事情是在绘制第一个轴之后添加第二个轴。 – James

+0

哇,它可以做! +1 – VMMF

1

在从MATLAB的这个例子是没有散射完成:

x = linspace(0,10); 
y1 = 200*exp(-0.05*x).*sin(x); 
y2 = 0.8*exp(-0.5*x).*sin(10*x); 
y3 = 0.2*exp(-0.5*x).*sin(10*x); 

figure 
[hAx,hLine1,hLine2] = plotyy(x,y1,[x',x'],[y2',y3']); 

通知,如果不是你写[hAx,hLine1,hLine2] = plotyy(x,y1,[x,x],[y2,y3]);你会失去第三的颜色,所以你需要[x',x']代替[x,x]

我刚刚测试了您的代码,但使用[ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3']);,我可以看到3种颜色。可悲的是,当我做[ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3'],'scatter');我继续得到同样

Error using scatter (line 44) 
X and Y must be vectors of the same length 

,甚至当我试图从MATLAB例子的代码。显然,分散属性不允许你有3组数据。如果您检查scatter.m的第42行,你会看到

[~, cax, args] = parseplotapi(varargin{:},'-mfilename',mfilename); 

当你执行[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2,'scatter');这是只有2集的情节,你会看到一行42 ARGS是如何与两个向量一个1x2的细胞,但是当您执行[ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3'],'scatter');这将是获得3种颜色

的唯一途径

(记住[ax,h1,h2]=plotyy(x1,y1,[x2,x3],[y2,y3],'scatter')只会给你2种颜色,虽然它的工作原理scatter.m内,args中,它也解释为2个载体)

像现在第42行中的参数是一个1x2的单元格,其中有两个矩阵,而不是两个向量产生错误。

而且使用:

blue=[0 0 1]; 
red=[1 0 0]; 
set(h1,'cdata',red); 
set(h2,'cdata',blue); 
set(ax(1),'ycolor','r'); 
set(ax(2),'ycolor','b'); 

是没有帮助的,你只能处理2而不是三个独立的轴。所以我想你的问题的答案是没有它不能完成(虽然如果你删除了散射约束它会)。

+0

感谢您帮助调试。我将其标记为有用,尽管我不接受它作为答案。 – James

+0

这很好,我很高兴你终于可以做到了 – VMMF