# Scatter plot from two lists with BarLegend

I have two lists

listx = {};
Do[x = RandomReal[{0, 1}]; AppendTo[listx, x], {1000}];
listy = {};
Do[y = RandomReal[{0, 10}]; AppendTo[listy, y], {1000}];


I can make a scatter plot as

data = Transpose@{listx, listy};
plotxy = ListPlot[data]


But in this scater plot, I want to add BarLegend in such a way that the point (0.5,5) has the darkest color and as the points start to deviate from this point the color gets lighter and lighter.

• Not an answer but you can generate 1000 random numbers, simply by: listx = RandomReal[{0, 1}, 1000]; and listy = RandomReal[{0, 10}, 1000];. – Mahdi May 27 '15 at 4:13

data3 = Style[{##},
ColorData["Rainbow"][Abs[(#1 - 0.5)] + Abs[1/10 (#2 - 5)]]] & @@@
data;
Legended[ListPlot[data3, PlotStyle -> PointSize[0.01]],
BarLegend["Rainbow"]]


Update for your question in the comment:

The color function ColorData["Rainbow"] ranges from 0 to 1 so the value has to be used within this range. Abs function basically works as a Ramp function in the two directions and centered at the center of the plot.

Row[{Plot3D[Abs[(x - 0.5)] + Abs[1/10 (y - 5)],{x,0,1},{y,0,10}],ContourPlot[Abs[(x - 0.5)] + Abs[1/10 (y - 5)], {x, 0, 1}, {y, 0, 10},
ContourLabels -> (Text[Style[#3, Bold, 14, Red], {#1, #2}] &),


You can see in this plot that Abs[(#1 - 0.5)] + Abs[1/10 (#2 - 5)] is within 0 and 1 for the range of the plot and it drops in the four directions. So when you apply ColorData["Rainbow"] for the values resulted from Abs[(#1 - 0.5)] + Abs[1/10 (#2 - 5)] you will get the color gradient that you want.

• Another option may be to use GrayLevel instead of ColorData["Rainbow"], to generate a gradient from black (closest) to white (furthest away). (+1) – MarcoB May 27 '15 at 5:24
• @Algohi, nice answer, thanks a lot. could you please explain this part of the code "Abs[(#1 - 0.5)] + Abs[1/10 (#2 - 5)]". i am confused because for the actual code, the lists do not obey such simple structure that i can easily rescale by 1/10. how to rescale lists for example: listx= {min(x), ...,x0,.., max(x) } listy= {min(y), ...,y0,.., max(y) } here, {x0,y0} is the point with the darkest point and other points as go away from it gets lighter. – SAS May 28 '15 at 4:29
• @SAS see the update – Algohi May 28 '15 at 4:49
• @ Algohi, excellent !!! but i need to mention one thing, for the example we are considering, Abs[(x - x0)/maxx] + Abs[(y - y0)/maxy does work approximately, but it should be properly normalized for general purpose to work and the correct formula should be Abs[(x - x0)/maxx]/Sqrt@2 + Abs[(y - y0)/maxy]/Sqrt@2 , which works for any combination. you may edit it. thank you again, such a nice answer :-) – SAS May 28 '15 at 6:02
data = Transpose[{RandomReal[{0, 1}, 10000],
RandomReal[{0, 10}, 10000]}];
Legended[Graphics[{Function[{x,
y}, {ColorData["Rainbow"][
Norm[{x, y/10} - {0.5, 0.5}]/Sqrt[0.5]], Point[{x, y}]}] @@@
data, {Red, PointSize[0.04], Point[{0.5, 5}]}},
AspectRatio -> Full, Frame -> True], BarLegend["Rainbow"]]