2016-04-14 9 views
1

私は検索しましたが解決策は見つかりませんでした。私の目標は、それぞれ異なる色で設定された3セットのデータを使って散布図をプロットすることです。ここに私のコードの例です:[×2 Y2] [×3 Y3]は同じ色を持っているよう二重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'); 

しかし、これは、私が欲しい、まさに何です。 3つのデータセットの色が異なるように色を変更する方法はありますか?また、3つのデータセットを示す凡例を追加する方法もあります。 MATLABからこの例では

+1

コードの間違いを修正してください。投稿したとおりには動作しません。それはplotyyのエラーと言う> fevalfun(361行目) – VMMF

+0

申し訳ありません、それは今更新され、動作しています。 – James

答えて

2

あなたがすることにより、独自のplotyyを構築することにより、plotyyの制限を克服することができます

  • はそのpositionを作る:figure
  • axes
  • axesを追加しての追加を作成

    • 第1軸の第1軸に等しい。
    • makそれは透明今、あなたは最初の引数(Eとして指定することによりscatterを使用する上での軸を選択することができ、そのcolor

「なし `に設定することにより、電子。 g。 scatter(axes_1, ...))。

すべてのデータセットをプロットした後、2軸の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

詳細な手順をお寄せいただきありがとうございます。私はこのアプローチを大規模なデータセットに適用することができ、意図したとおりに動作します。私が変更した唯一の事は、最初のものをプロットした後に2番目の軸を追加することです。 – James

+0

うわーできました! +1 – VMMF

1

それは、散乱せずに行われます。

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');を行うときに悲しいことに私はMathWorks社のMATLABの例からのコードにしようとした場合でも、同じ

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

を得続けます。明らかに、scatterプロパティでは3つのデータセットを持つことができません。あなたがscatter.mのライン42をチェックすると、あなたがライン42で引数2つのベクトルを持つ1x2のセルで、まだ時にどのように表示されますのみ2セットのプロットである[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2,'scatter');を実行するとき、あなたは

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

が表示されますあなたは今のように3色

[ax,h1,h2]=plotyy(x1,y1,[x2,x3],[y2,y3],'scatter')を覚えて、それはまた、2つのベクトルとして解釈される引数のようにscatter.m内で動作が唯一のあなたの2色を与える)

を得るための唯一の方法だろう[ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3'],'scatter');を実行します42行目のargsは、間違いになる2つのベクトルの代わりに2つの行列を持つ1x2セルです。また使用

:あなたが唯一の2の代わりに、三つの独立した軸を操作できるよう

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'); 

は役に立ちません。だから私はあなたの質問への答えは、それが行われることはできません(あなたが散乱制約を削除した場合でも)ことはできないと思います。

+0

ありがとうございました。私はそれを有用とマークしましたが、私は答えとしてそれを受け入れませんでした。 – James

+0

それは大丈夫です最終的にそれを作ることができてうれしいです – VMMF

関連する問題