带有彩色组和组内不同标记的散点图 [英] Scatter plot with coloured groups and different markers within a group
问题描述
我正在尝试制作一个由不同群体组成的散点图.除此之外,我还要为每2个点设置2个不同的标记和一种颜色,这些标记也与一条线相连.但详情请参见下文
I am trying to make kind of a scatter plot with different groups. In addition to this I would like to have 2 different markers and one color for each set of 2 points, which are also connected with a line. But see below for details
我有4个矩阵
Db = [0.4745 0.3886 0.3316 0.2742; 0.5195 0.3825 0.3341 0.2846; 0.4929 0.3951 0.3161 0.2918; 0.4905 0.4052 0.3240 0.2882];
Dw = [0.4814 0.3905 0.3418 0.2922; 0.5258 0.3952 0.3420 0.2974; 0.4945 0.4012 0.3386 0.3001; 0.4885 0.4076 0.3382 0.3056];
Sb = [0.0476 0.0527 0.0543 0.0592; 0.0432 0.0503 0.0521 0.0592; 0.0460 0.0531 0.0536 0.0508; 0.0488 0.0520 0.0542 0.0543];
Sw = [0.0693 0.0738 0.0785 0.0839; 0.0642 0.0731 0.0763 0.0862; 0.0670 0.0755 0.0807 0.0753; 0.0744 0.0733 0.0792 0.0776];
我想将它们绘制为散点图,其中Sb
针对Db
,Sw
针对Dw
.但是现在我希望它们具有不同的标记,以便Sb
/Db
点具有'x',而Sw
/Dw
点具有'o'.
I would like to plot them as a scatter plot with Sb
against Db
and Sw
against Dw
. But now I would like them to have different markers so that the Sb
/Db
points have an 'x' and Sw
/Dw
points have an 'o'.
然后我还想用一条线连接它们,例如,Sb
/Db
的第一个元素应该与Sw
/Dw
的第一个元素连接.
Then additionally I want to connect them with a line, so for example the first element of Sb
/Db
should be connected with the first element of Sw
/Dw
.
这样的事情(在此示例的图形编辑器中进行了编辑...)
Something like this (edited in a graphics editor for this example...)
我已经尝试过gscatter
gscatter([Db(:)' Dw(:)'],[Sb(:)' Sw(:)'],[1:16 1:16])
但是,我不知道如何更改标记或添加行.
But with this I don't know how to change the markers or add lines.
有人可以帮我吗?
推荐答案
您可以通过两次调用scatter
和一次调用line
来实现.
You can do this with a couple of calls to scatter
and one call to line
.
% Turn your data into 1D row vectors
vDb = Db(:).'; vDw = Dw(:).'; vSb = Sb(:).'; vSw = Sw(:).';
% Plotting
figure; hold on
% Scatters for points
scatter(vDb, vSb, 'kx'); % plotting with black (k) crosses (x)
scatter(vDw, vSw, 'ko'); % plotting with black (k) circles (o)
% Line to get lines!
line([vDb; vDw], [vSb; vSw], 'color', 'k') % Plot black (k) lines between 'b' and 'w' pts
输出:
通过使用多次调用line
而不是使用scatter
,为两个调用指定标记,但仅使用起点/终点,用NaN
替换另一个,您可以使每对颜色不同
You can get different colours per pair by just using multiple calls to line
instead of using scatter
, specifying the markers for two of the calls but only using the start/end points, replacing the other with NaN
.
% No need for 'hold on' as line doesn't clear the plot!
figure;
line([vDb; NaN.*vDw], [vSb; NaN.*vSw], 'marker', 'x') % Plot coloured x markers
line([NaN.*vDb; vDw], [NaN.*vSb; vSw], 'marker', 'o') % Plot coloured o markers
line([vDb; vDw], [vSb; vSw]) % Plot coloured lines between 'b' and 'w' pts
输出:
请注意,这使用默认的颜色集.可以使用
Note that this uses the default colour set. This can be changed by using
set(gca, 'colororder', mycolours)
其中mycolours
是3列RGB矩阵,如使用get(gca, 'colororder')
所示.
where mycolours
is a 3 column RGB matrix, as seen if you use get(gca, 'colororder')
.
这篇关于带有彩色组和组内不同标记的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!