简体   繁体   中英

Conditional coloring using graphics in Mathematica

Please consider:

dalist = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, 
          {1, 2, 1}, {2, 3, 1}, {3, 4, 1}, {4, 5, 1}, {5, 6, 1}}

I use the following to plot the above where #2 & #3 are x & y coordinates

Graphics@MapThread[Point[{#2, #3}] &, Transpose@dalist]

在此处输入图像描述

  • #1 a timing reference I would like to use to color the points.

  • It can range from 1 to 30 in my data.

  • #1 =1 should always yield to the same color.

EDIT: Building-up on solutions below

  • How could I set the color range/gradient manually or use an existing one ("BlackBodySpectrum")?

You can use ColorData :

Graphics@MapThread[{ColorData[1][#1], Point[{#2, #3}]} &, 
  Transpose@dalist]

Try:

datlist = Flatten[Table[{i, j + i, j}, {i, 1, 20}, {j, 1, 20}], 1];
colordata = 60; (* Try different palettes 1 .. 62 *)

Graphics[(Sequence @@ 
     {Directive[ 
      ColorData[colordata][Mod[#[[1]], ColorData[colordata, "Range"][[2]]]]], 
     PointSize -> Large, Point[{#[[2]], #[[3]]}]} & /@ datlist), 
     Frame -> True]

在此处输入图像描述

Edit

Using BlackBodySpectrum :

datlist = Flatten[Table[{i, j + i, j}, {i, 1, 20}, {j, 1, 20}], 1];
colordata = "BlackBodySpectrum";(*Try different palettes 1.. 62*)
Graphics[(
  Sequence @@ {Directive[
       ColorData[
         colordata][#[[1]] ColorData[colordata, 
            "Range"][[2]]/(Length@datlist/
            Length@Select[datlist, #[[1]] == 1 &])]], 
      PointSize -> Large, Point[{#[[2]], #[[3]]}]} & /@ datlist), 
 Frame -> True]

在此处输入图像描述

No need for MapThread

Graphics[{PointSize -> 0.05, {ColorData[1][#1], Point[{#2, #3}]} & @@@dalist}]

在此处输入图像描述

You can roll your own image gradients using Blend :

Graphics[{PointSize -> 0.05, 
         {Blend[{Red, Yellow, Blue}, #1/5], Point[{#2, #3}]} & @@@ dalist}]

Please note that you have to take care yourself of scaling your data range so that the maximum value generates 1 and the minimum 0 (if you want to optimally use the available range)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM