1
$\begingroup$

I've been searching for the use of ColorFunction as an Option for ListPlot, but haven't found anything about it in the documentation. When trying the code

ListPlot[
 RandomReal[5, {10}]
 , ColorFunction -> Hue[#1, #2, 1] &
 ]

I get the error message

ListPlot::nonopt: "Options expected (instead of ColorFunction->Hue[#1,#2,1]&) beyond position 3 in ListPlot[{2.14801,2.18933,<<6>>,3.48,0.566812},ColorFunction-><<1>>&]. An option must be a rule or a list of rules."

Is it possible, and if so, how, to use ColorFunction with ListPlot? What are the arguments of the function, then?

Thanks for all help, as always!

$\endgroup$
3
  • 1
    $\begingroup$ The problem here is that you haven't used parentheses to group the function. It should be ColorFunction -> (Hue[#, #2, 1]&). See Parentheses in pure functions: # & vs. ( # &) and Using several anonymous functions mixed together for more info. Regarding your actual question, ListPlot doesn't accept a ColorFunction without Joined -> True (or ListLinePlot). Your solution is to use either of these and convert the lines to points as in this answer $\endgroup$
    – rm -rf
    Oct 31, 2013 at 18:47
  • $\begingroup$ Either way, it's a duplicate of one of these — you can decide which :) $\endgroup$
    – rm -rf
    Oct 31, 2013 at 18:48
  • $\begingroup$ @rm -rf: Thanks for the link, that's great! $\endgroup$
    – Gabriel
    Oct 31, 2013 at 19:17

1 Answer 1

5
$\begingroup$

From the docs for the ColorFunction option to ListPlot:

ColorFunction requires at least one dataset to be Joined

{ListPlot[Sinc[Range[0, 10, 0.1]],
          ColorFunction -> Function[{x, y}, Hue[x]]],
 ListPlot[Sinc[Range[0, 10, 0.1]],
          ColorFunction -> Function[{x, y}, Hue[x]], Joined -> True]}

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ This appears to have changed in v12.0 -- now both give rainbow colors. $\endgroup$
    – Chris K
    May 4, 2021 at 3:23

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