更改具有双 y 轴的散点图的颜色
change colors of a scatter plot with double y axis
我四处寻找,但没有找到解决办法。我的目标是绘制一个包含 3 组数据的散点图,每组数据具有不同的颜色。这是我的代码示例:
%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] 具有相同的颜色。有没有办法改变颜色,让三组数据有不同的颜色?以及如何添加显示三组数据的图例?
在这个来自 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 行中的 args 是一个 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 个而不是三个独立的轴。所以我猜你的问题的答案是不可以(尽管如果你删除分散约束它会)。
您可以通过以下方式构建自己的 plotyy 来克服 plotyy
的限制:
- 创建
figure
- 添加一个
axes
- 再添加一个
axes
并且:
- 使其
position
等于第一个轴
- 通过将其
color
设置为 `none" 使其透明
现在您可以 select 通过将其指定为第一个参数(例如 scatter(axes_1, ...)
)来使用 scatter
的轴。
绘制完所有数据集后,您必须使两个轴的 xlim
相等。
要添加图例,您只需指定 handles" returned by the
scatterfunction as first argument of the
legend` 函数。
此方法已在以下代码中实现。
在代码中进行检查以验证是否可以使用 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')
希望这对您有所帮助。
Qapla'
我四处寻找,但没有找到解决办法。我的目标是绘制一个包含 3 组数据的散点图,每组数据具有不同的颜色。这是我的代码示例:
%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] 具有相同的颜色。有没有办法改变颜色,让三组数据有不同的颜色?以及如何添加显示三组数据的图例?
在这个来自 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 行中的 args 是一个 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 个而不是三个独立的轴。所以我猜你的问题的答案是不可以(尽管如果你删除分散约束它会)。
您可以通过以下方式构建自己的 plotyy 来克服 plotyy
的限制:
- 创建
figure
- 添加一个
axes
- 再添加一个
axes
并且:- 使其
position
等于第一个轴 - 通过将其
color
设置为 `none" 使其透明
- 使其
现在您可以 select 通过将其指定为第一个参数(例如 scatter(axes_1, ...)
)来使用 scatter
的轴。
绘制完所有数据集后,您必须使两个轴的 xlim
相等。
要添加图例,您只需指定 handles" returned by the
scatterfunction as first argument of the
legend` 函数。
此方法已在以下代码中实现。
在代码中进行检查以验证是否可以使用 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')
希望这对您有所帮助。
Qapla'