Changing the colors of a scatter diagram with double axis y

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?

You can overcome the limitation of plotyy by building your own plotyy by:

  • create a figure
  • add an axes
  • add a second axes and:
    • make its position equal to th eone of the first axes
    • make it transparent by setting its color 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 scatter function 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’

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Changing the colors of a scatter diagram with double axis y

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录