2016-04-14 150 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