3

I'm trying to use ColorFunction on a scatter plot of data ; I would like the point's opacity to go up with the y axis and the level of "Black" on the CMYK color to be a function of the x axis.

However, I don't know how to proceed, as this is real data and not a generated plot. Thanks.

E Bassal
  • 746
  • 4
  • 11
  • 1
    How are you visualizing the data? Using a ListPlot? I also don't understand the distinction about real data - you should be able to use the same techniques on real or fake data :-) – Jason B. Aug 26 '16 at 17:45
  • Yes I'm using ListPlot. Haha what I meant was I'm trying to display data instead of functions! Because in the documentation there's a whole lot of examples about using ColorFunction with something like f[x_], but not with actual data points. What you mean Mathematica doesn't know if the data is garbage or not? ;) – E Bassal Aug 26 '16 at 18:57
  • I was just trying to be funny. See the listColorPlot below - you'll have to fiddle around with the color function to get it how you want it. It should be a pure function that takes 2 arguments, #1 is the x coordinate and #2 is the y coordinate. – Jason B. Aug 26 '16 at 19:00
  • Alright, thanks a lot – E Bassal Aug 26 '16 at 19:13

1 Answers1

4

I will assume you are looking to do a ListPlot using a ColorFunction. In the absence of the real data, let's use some fake data.

SeedRandom[42];
data = RandomReal[1, {20, 2}];

It seems to me that this should work,

ListPlot[data, ColorFunction -> (Directive[Opacity[#1], Hue[#2]] &)]

Mathematica graphics

But you can see that it doesn't. That isn't a bug, it's as designed. ColorFunction is only going to be applied to Line objects,

ListPlot[data, ColorFunction -> (Directive[Opacity[#2], Hue[#1]] &), 
 Joined -> True]

Mathematica graphics

The documentation gives a hint about how to add colors to the points by using Style,

ListPlot[Evaluate[
  Style[#, Opacity[Last@#], Hue[First@#]] & /@ data]
 ]

But you can see that the opacity is ignored, even though that is accepted syntax for Style (verify by running Text[Style["AbBbCc", Blue, Opacity[.2]]]).

So we are left with creating our own ListPlot using Graphics primitives

Graphics[({Directive[Opacity[Last@#], Hue[First@#]], PointSize[Large],
      Point@#} &) /@ data, Frame -> True]

Mathematica graphics

So that works, but it's a pain to use. We can wrap it up into a function via

listColorPlot[data_, colorfunction_: (Hue[#2] &), 
  opts : OptionsPattern[]] := Module[
  {colors},
  colors = colorfunction @@@ Rescale[data];
  Graphics[GraphicsComplex[data,
    {PointSize[0.02],
     Thread[{colors, Point /@ Range[Length@data]}]}],
   opts,
   Frame -> True,
   AspectRatio -> 1/GoldenRatio]
  ]

Here is an example of how to run it,

data = RandomReal[{-10, 20}, {20, 2}];
listColorPlot[data, (Directive[Opacity[#2], Hue[#1]] &)]

Mathematica graphics

edit

A simple way to do this, from Heike's answer in the duplicate, is to simply apply a replacement rule like

ListPlot[data, ColorFunction -> (Directive[Opacity[#2], Hue[#1]] &), 
  Joined -> True] /. Line -> Point

Mathematica graphics

Jason B.
  • 68,381
  • 3
  • 139
  • 286