Reputation: 99
I've searched around, but have not found a solution. My goal is to plot a scatter plot with 3 sets of data, each set with a different color. Here is an example of my code:
%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');
However, this is what exactly what I want, as [x2 y2] [x3 y3] has the same color. Is there a way to change the colors so that the three set of data has different colors? And also how to add a legend showing the three sets of data?
Upvotes: 1
Views: 706
Reputation: 5175
You can overcome the limitation of plotyy
by building your own plotyy by:
figure
axes
axes
and:
position
equal to th eone of the first axescolor
to `none"Now you can select the axes on which to use scatter
by specifying it as a first argument (e. g. scatter(axes_1, ...)
).
After having plot all the data set, you have to make the xlim
of the two axes equals.
To add the legend, you just have to specify the handles" returned by the
scatterfunction as first argument of the
legend` function.
This approach has been implemented in the following code.
In the code a check is made in order to verify if it is possible to use 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')
Hope this helps.
Qapla'
Upvotes: 2
Reputation: 954
In this example from matlab it is done without the scatter:
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']);
notice that if instead you write [hAx,hLine1,hLine2] = plotyy(x,y1,[x,x],[y2,y3]);
you'll loose the third color, so you need the [x',x']
instead of the [x,x]
I just tested your code but with [ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3']);
and I could see the 3 colors. Sadly when I do [ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3'],'scatter');
I keep getting the same
Error using scatter (line 44)
X and Y must be vectors of the same length
, even when I tried on the code from the matlab example. Apparently the scatter property does not allow you to have 3 set of data. If you check line 42 of scatter.m you'll see
[~, cax, args] = parseplotapi(varargin{:},'-mfilename',mfilename);
When you execute [hAx,hLine1,hLine2] = plotyy(x,y1,x,y2,'scatter');
which is only a 2 set plot you'll see how args in line 42 is a 1x2 cell with two vectors, yet when you execute [ax,h1,h2]=plotyy(x1,y1,[x2',x3'],[y2',y3'],'scatter');
which would be the only way of getting 3 colors
(remember [ax,h1,h2]=plotyy(x1,y1,[x2,x3],[y2,y3],'scatter')
will only give you 2 colors although it works within scatter.m as in args that is also interpreted as 2 vectors)
as now args in line 42 is a 1x2 cell with two matrices instead of two vectors that yields to a mistake.
Moreover using:
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');
Isn't helpful as you can only manipulate 2 instead of three independent axis. So I guess that the answer to your question is NO IT CAN'T BE DONE (Although if you remove the scatter constraint it will).
Upvotes: 1