Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring plot in Mathematica according to labels

I have a dataset with labels which I would like to plot with points colored according to their label. Is there a simple way how to get current line numer inside plot, so that I can determine which category does the point belong to?

I understood that x,y,z are the coordinates of plotted data, but it doesn't help for the external labels.

This is quite ugly and it works just on sorted dataset with regular distribution.

    data = Import["http://ftp.ics.uci.edu/pub/machine-learning-databases/iris/iris.data"];
    data = Drop[data, -1]; (*there one extra line at the end*)
    inData = data[[All, 1 ;; 4]];
    labels = data[[All, 5]];
    ListPlot3D[inData,
      ColorFunction -> 
        Function[{x, y, z}, 
          If[y < 0.33, RGBColor[1, 1, 0.], 
               If[y < 0.66, RGBColor[1, 0, 0.], RGBColor[1, 0, 1]]
          ]
        ]
    ]

Expected result:

expected result of visualization

like image 882
Tombart Avatar asked Oct 29 '11 18:10

Tombart


People also ask

How do you add labels to a Mathematica plot?

To add a centered label to the top of a plot, use the PlotLabel option: Copy to clipboard. You can control the appearance of the label by wrapping it in Style options: Copy to clipboard.

What is plot label?

PlotLabel->label specifies a label to give. Any expression can be used as a label. It will be given by default in TraditionalForm. Arbitrary strings of text can be given as "text". » PlotLabel->StandardForm[expr] will give a label in standard Wolfram Language form. »


2 Answers

Suppose that points is the lists of coordinates and labels a list of the corresponding labels so for example

points = Flatten[Table[{i, j, Sin[i j]}, 
   {i, 0, Pi, Pi/20}, {j, 0, Pi, Pi/10}], 1];
labels = RandomChoice[{"label a", "label b", "label c"}, Length[points]];

Each label corresponds to a colour which I'm writing as a list of rules, e.g.

rules = {"label a" -> RGBColor[1, 1, 0], 
   "label b" -> RGBColor[1, 0, 0], "label c" -> RGBColor[1, 0, 1]};

Then the points can be plotted in the colour corresponding to their label as follows

ListPointPlot3D[Pick[points, labels, #] & /@ Union[labels], 
   PlotStyle -> Union[labels] /. rules]

plot

Edit

To colour individual points in a ListPlot3D you can use VertexColors, for example

ListPlot3D[points, VertexColors -> labels /. rules, Mesh -> False]

colouring points in a ListPlot3D

like image 86
Heike Avatar answered Sep 18 '22 20:09

Heike


For Example:

(* Build the labeled structure and take a random permutation*)
f[x_, y_] = Sqrt[100 - x x - y y];
l = RandomSample@Flatten[{Table[{{"Lower", {x, y, f[x, y] - 5}},
                                 {"Upper", {x, y, 5 - f[x, y]}}},
                          {x, -5, 5, .1}, {y, -5, 5, .1}]}, 3];
(*Plot*)

Graphics3D[
 Riffle[l[[All, 1]] /. {"Lower" -> Red, "Upper" -> Green}, 
  Point /@ l[[All, 2]]], Axes -> True]

enter image description here

like image 20
Dr. belisarius Avatar answered Sep 17 '22 20:09

Dr. belisarius