scatter plot with 4 different background color

20 ビュー (過去 30 日間)
Jake
Jake 2015 年 4 月 16 日
回答済み: Star Strider 2015 年 4 月 16 日
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

回答 (2 件)

Star Strider
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:

Oliver Woodford
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.

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by