0
$\begingroup$

Here is a sample data file

m = {{-10,3,1},{-7.5,12,3},{-1.5,5,2},{2,4,4},{3,7,4},{6,9,5},{7,1,5},
     {8,3,1},{9,12,2},{10,8,5}};

The first element of the list is a point on the x-axis, while the other two are integers for other information.

Now let's create a color code depending on the third integer element

getColor[m_List, i_Integer] := Module[{s = m[[i, 3]]}, 
Which[s == 0, Black, s == 1, Darker[Green], s == 2, Red, s == 3, 
Blue, s == 4, Magenta, s == 5, Orange, s == 6, Cyan, s == 7, 
Brown]];

And let's plot the color points on the x-axis

data = Table[{PointSize[0.001], getColor[m, i], Point[{m[[i, 1]], 0}]},
       {i, 1, Length[m]}];
S0 = Graphics[data]

enter image description here

The above plot does not correspond to the sample data but to the actual data file. Nevertheless we see that the colors are hardly visible since all points lie on the x-axis.

I would like the following: Get some height. In particular, for every color point $x_0$ to create a vertical line at $x_0$ with the same color let's say from $y_{min} = -1$ up to $y_{max} = 1$.

Any suggestions?

Many thanks in advance!

$\endgroup$
  • 1
    $\begingroup$ Try Line? p.s. Which -> Switch will shorten your code here. $\endgroup$ – Kuba Dec 11 '15 at 10:36
  • $\begingroup$ @Kuba You mean data = Table[{Thickness[0.001], getColor[m, i], Line[{{m[[i, 1]], -1}, {m[[i, 1]], 1}}]}, {i, 1, Length[m]}]; right? How can Which could be substituted with Which? $\endgroup$ – Vaggelis_Z Dec 11 '15 at 10:43
3
$\begingroup$
    m = {{-10, 3, 1}, {-7.5, 12, 3}, {-1.5, 5, 2}, {2, 4, 4}, {3, 7, 4}, {6, 9, 5}, {7, 1, 5}, {8, 3, 1}, {9, 12, 2}, {10, 8, 5}};

    colors = {Black, Darker[Green], Red, Blue, Magenta, Orange, Cyan, Brown};

    data = Table[{colors[[m[[i, 3]]]], Line[{{m[[i, 1]], -1}, {m[[i, 1]], 1}}]}, {i, 1, Length[m]}];
    S0 = Graphics[data]

enter image description here

Or more compactly (you don't need Table):

    Graphics[{colors[[#[[3]]]], Line[{{#[[1]], -1}, {#[[1]], 1}}]} & /@ m]

EDIT for Jason B:

The graphic was made with the following additional options:

    Frame -> True, AspectRatio -> 1/2, PlotRangePadding -> None
$\endgroup$
  • $\begingroup$ When I type in your code, I don't see a frame at all, same with OP's code - I just see a bunch of really small dots for his. Am I doing something wrong? $\endgroup$ – Jason B. Dec 11 '15 at 10:47
  • $\begingroup$ @JasonB Nothing wrong at all! The plot I posted was created using more options like Frame etc. $\endgroup$ – Vaggelis_Z Dec 11 '15 at 10:49
  • $\begingroup$ @JasonB Sorry I added the options after I'd copied the code. See my edit. $\endgroup$ – Quantum_Oli Dec 11 '15 at 10:50
  • $\begingroup$ When I use your code the results do not match. What you define colors = {Black, Darker[Green], Red, Blue, Magenta, Orange, Cyan, Brown}; does black correspond to option 1 or 0? $\endgroup$ – Vaggelis_Z Dec 11 '15 at 10:55
  • 1
    $\begingroup$ Ah my bad, yeah I had assumed your colour values went from 1, not 0. Just change colours[[#[[3]]]] to colours[[#[[3]]+1]] $\endgroup$ – Quantum_Oli Dec 11 '15 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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