![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176151/image.png)
scatter plot with 4 different background color
10 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149130/image.png)
Hi MATLAB experts,
I have a quick question regarding background color in a scatter plot as attached. For example, I have code as follows.
a = rand(10,1);
b = rand(10,1);
scatter(a,b)
In this scatter plot, I'd like to have 4 different color for each territory with annotations that can show the counts in each territory. Could you please share your thoughts on this???
Thanks in advance!!!
Regards, Jake
0 个评论
回答(2 个)
Star Strider
2015-4-16
Interesting problem!
This works, but it is restricted to the problem and the plot coordinates in your Question. You could generalise it to other problems, but I will leave that to you. The ‘coords’ vector I created to help me define where to put the text objects. I use the last two elements to determine where (coordinates) to plot them. The ‘S’ matrix is the array of counts, and simply sums the logical vector created by the conditional statements in it to create the counts for each quadrant. The documentation for patch explains how those work.
The axis call is an important part of the code because it defines the axis limits so that the patch objects display correctly.
Experiment with the colours of the patch objects, the marker colours, and the text object font to create the sort of plot you want.
The Code:
a = rand(10,1);
b = rand(10,1);
figure(1)
patch([0 0.5 0.5 0], [0 0 0.5 0.5],'y')
hold on
patch([0.5 1.0 1.0 0.5], [0 0 0.5 0.5],'g')
patch([0.5 1.0 1.0 0.5], [0.5 0.5 1.0 1.0],'b')
patch([0 0.5 0.5 0], [0.5 0.5 1.0 1.0],'r')
scatter(a, b, 'k')
hold off
axis([0 1 0 1])
for k1 = 1:2
for k2 = 1:2
S(k1,k2) = sum((a >= (k1-1)*0.5) & (a <= k1*0.5) & (b >= (k2-1)*0.5) & (b <= k2*0.5));
coords = [k1, k2, (k1-1)*0.5+0.25 (k2-1)*0.5+0.25];
text(coords(3), coords(4), sprintf('# = %2d', S(k1,k2)))
end
end
The Plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176151/image.png)
0 个评论
Oliver Woodford
2015-4-16
编辑:Oliver Woodford
2015-4-16
figure;
im = rand(2, 2, 3);
image([0 1], [0 1], im);
hold on
a = rand(10,1);
b = rand(10,1);
scatter(a, b);
Then use text() for the annotations.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!