4
$\begingroup$

I am working with a list of points on the plane, to each of them a natural number is assigned. It will be responsinble for the points' colors.

1) My first question is very easy: at initial moment I create list of random points and assign number 1 to all of them. I am doing this, probably, too ugly:

listOfPoints = RandomReal[{0,10}, {1000, 2}];
listOfPoints = Map[Append[#, 1] &, listOfPoints];
ListPlot[Map[Take[#, 2] &, listOfPoints]]

(Here colors still does not play.) Does exist a more elegant way?

2) And after I make some manipulations with some elements of the list, each manipulation will increase the number assigned to the element, i.e. its last coordinate. And then I would like to produce ListPlot in such a way that the color of a point {x,y,n} from the list will be, say,

Blend[{{Blue}, {Red}}, 1/n]

How do realize this?

$\endgroup$

3 Answers 3

3
$\begingroup$

First of all, it is better to store coordinates (real numbers) and integer numbers separately. You will benefit from packed arrays

n = 1000;
points = RandomReal[{0, 10}, {n, 2}];
numbers = ConstantArray[1, n];

After some data monipulation you will have different numbers. You can plot them easily with Point with specified VertexColors

numbers = RandomInteger[{1, 10}, n];

Graphics[Point[points, VertexColors -> (Blend[{Blue, Red}, 1/#] & /@ numbers)], 
 Frame -> True]

enter image description here

$\endgroup$
2
$\begingroup$
listOfPoints = MapIndexed[Join[#1, #2] &, RandomReal[{0, 10}, {1000, 2}]];
Graphics[{Blend[{Blue, Red}, 1/#3], Point[{#1, #2}]} & @@@ listOfPoints, Axes -> True]

enter image description here

Unfortunately, the blending very quickly results in basically all Blue (you can see the first point in red in the upper right). The problem is illustrated here:

Blend[{Blue, Red}, #] & /@ Table[1/n, {n, 1, 10}]

enter image description here

You might want to choose a different function to get a smoother transition from Red to Blue. Perhaps a linear interpolation:

listOfPoints = MapIndexed[Join[#1, #2] &, RandomReal[{0, 10}, {1000, 2}]];
Graphics[{Blend[{Blue, Red}, (1000 - #3 + 1)/1000], Point[{#1, #2}]} & @@@ listOfPoints, Axes -> True]

enter image description here


(I like MapIndexed, due to its flexibility: you can actually put the function directly in there as, for instance,

listOfPoints = MapIndexed[Join[#1, 1/#2] &, RandomReal[{0, 10}, {1000, 2}]];

but of course you can always do just

listOfPoints = Transpose@Join[Transpose@#, {Range[Length@#]}] &@ RandomReal[{0, 10}, {1000, 2}];
$\endgroup$
2
$\begingroup$

You might consider to use ListPointPlot3D for this kind of problem

points = 10;
colors = 3;

data =
  Join[
   RandomReal[{0, 10}, {points, 2}],
   List /@ RandomInteger[{1, colors}, points],
   2];

data // TableForm

enter image description here

plot =
 ListPointPlot3D[
  data, 
  PlotStyle -> PointSize @ Large,
  ColorFunction -> "Rainbow"]

enter image description here

If you want you can use ColorFunction -> (Blend[{Red, Blue}, #3] &)

Let it look like a ListPlot

Show[
 plot,
 AspectRatio -> 1/GoldenRatio,
 Axes -> {True, True, False},
 Boxed -> False,
 FaceGrids -> {{0, 0, 1}, {0, 0, 1}},
 ViewPoint -> {0, 0, Infinity}]

enter image description here

Compare

ListPlot[Most /@ data, GridLines -> Automatic]

enter image description here

View it from front

Show[
 plot,
 Axes -> {False, True, True},
 Boxed -> False,
 FaceGrids -> {{1, 0, 0}, {1, 0, 0}},
 ViewPoint -> {Infinity, 0, 0}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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