scatter plot with 4 different background color
10 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 16 Apr 2015
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:
0 Comments
Oliver Woodford
on 16 Apr 2015
Edited: Oliver Woodford
on 16 Apr 2015
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 Comments
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!