1
$\begingroup$

I am fairly new to mathematica, I have been trying to find some inbuilt plotting function that lets us plot a scatter plot of a dataset: (x,y,f(x,y)), where color of the plot is based on value of f(x,y); something like matplotlib.pyplot.scatter; for example if I have a dataset: data= {{1,2,3},{4,5,6},...,{1001,1002,1003}}; and I would like to make a plot like in ListPlot and color the data based on #[[3]]&/@ data; Thanks in advance.

$\endgroup$
1
  • $\begingroup$ You may find this helpful. $\endgroup$
    – bbgodfrey
    Commented Dec 28, 2021 at 13:40

2 Answers 2

2
$\begingroup$
Clear["Global`*"]

SeedRandom[1234];

data = {#[[1]], #[[2]], #[[1]]^2 + #[[2]]/2} & /@
   RandomReal[5, {100, 2}];

funcRange = MinMax[data[[All, 3]]];

Legended[
 Graphics[{
   AbsolutePointSize[4],
   {ColorData["Rainbow"][
       Rescale[#[[3]], funcRange]],
      Point[Most@#]} & /@ data},
  Frame -> True,
  FrameLabel -> (Style[#, 14] & /@ {x, y})],
 BarLegend[{"Rainbow", funcRange},
  LegendLabel -> Style["f", 14]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks Bob, this works great, I was exactly looking for this kind of solution. $\endgroup$
    – mjm
    Commented Dec 29, 2021 at 6:04
2
$\begingroup$
t = Partition[Range[1, 1003], 3];

ListPointPlot3D[t, ColorFunction -> (ColorData["Rainbow"][#3] &)]

enter image description here


EDIT-1

The closest visualization I can think of for 2D is a ListContourPlot, otherwise it will be a grid of points with different colors.

ListContourPlot[
 Table[Sin[x] Cos[y], {x, 0, 2 \[Pi], \[Pi]/50}, {y, 0, 
   2 \[Pi], \[Pi]/50}], ColorFunction -> "Rainbow"]

enter image description here


EDIT-2

t4 = Array[(Sin[(2 \[Pi] #1)/100] Cos[(2 \[Pi] #2)/100]) &, {100, 
    100}] // N

ArrayPlot[t4, ColorFunction -> "Rainbow", Frame -> True, 
 FrameTicks -> Automatic]

enter image description here


EDIT-3

Using ListPlot and creating a mock example conforming to a Binormal distribution.

PDF[BinormalDistribution[{50, 50}, {20, 20}, 0], {x, y}]

$$\frac{\exp \left(\frac{1}{2} \left(-\frac{1}{400} (x-50)^2-\frac{1}{400} (y-50)^2\right)\right)}{800 \pi }$$

Define a function and generate dataxy randomly.

fz[{x_, y_}] := 
  PDF[BinormalDistribution[{50, 50}, {20, 20}, 0], {x, y}];

dataxy = RandomVariate[BinormalDistribution[{50, 50}, {20, 20}, 0], 
   1000];

dataz = Rescale[fz[#] & /@ dataxy];

Generate the list of 3-tuples:

alist = MapThread[Flatten[Join[{#1, #2}]] &, {dataxy, dataz}];

Before we color the points, let's see what these look like:

ListPlot3D[alist]

enter image description here

Give each point a color based on #[[3]]

ListPlot[Style[#[[{1, 2}]], Hue@#[[3]]] & /@ alist
 , PlotStyle -> PointSize[0.007], 
 PlotLegends -> BarLegend[{Hue, {0, 1}}]]

enter image description here

I have benefitted from this answer by @becko for the final part.

$\endgroup$
4
  • $\begingroup$ Thank you for your suggestion Syed; Is it possible get this done in 2D as well? $\endgroup$
    – mjm
    Commented Dec 28, 2021 at 12:43
  • $\begingroup$ Thanks for suggesting ListContourPlot, however as you have mentioned is this possible to make 2d representation like a grid of points with different colors, if possible how can I plot that? $\endgroup$
    – mjm
    Commented Dec 28, 2021 at 13:11
  • $\begingroup$ Thanks Syed, both the ListPlot and ArrayPlot solutions are really useful for my case, only problem is that I have a dataset with ~40000 points, so the ListPlot solution provided by you takes a long time to run $\endgroup$
    – mjm
    Commented Dec 29, 2021 at 6:05
  • $\begingroup$ I would suggest that you start a fresh post and use the performance-tuning tag to address speed issue. There are many Graphics solutions on this page and if none of them have worked for your case then explain what you are looking for in a fresh post. $\endgroup$
    – Syed
    Commented Dec 29, 2021 at 6:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.