8
$\begingroup$

Bug introduced in 10.3.0 and persisting through 13.0 or later


This works:

ListPlot[Range[-10, 10], ColorFunction -> (If[#2 > 0, Red, Green] &), 
 ColorFunctionScaling -> False]

but as soon as I specify custom PlotMarkers, the ColorFunction gets ignored:

ListPlot[Range[-10, 10], ColorFunction -> (If[#2 > 0, Red, Green] &), 
 ColorFunctionScaling -> False, PlotMarkers->"\[Cross]"]

What am I doing wrong? (I use Mathematica 12 on Ubuntu)

$\endgroup$
5
  • 2
    $\begingroup$ Simplest workaround for this specific case is to split the data into two parts and plot them as two datasets. Then use PlotStyle for the colour. $\endgroup$
    – Szabolcs
    Oct 11, 2019 at 13:35
  • 1
    $\begingroup$ related issue/(bug?) with PlotMarkers in v12: PlotMarkers -> Automatic makes all markers Black regardless of PlotStyle or ColotFunction specification. Compare ListPlot[{Range[-10, 10], 2 Range[-10,10]}] and ListPlot[{Range[-10, 10], 2 Range[-10,10]}, PlotMarkers->Automatic] $\endgroup$
    – kglr
    Oct 11, 2019 at 16:09
  • 1
    $\begingroup$ @kglr With version 12.0 on Windows 7 x64 I do not observe this: both set of markers have colors as expected. But in the Cloud I do reproduce the bug. $\endgroup$ Oct 12, 2019 at 8:14
  • 2
    $\begingroup$ @AlexeyPopkov, just noticed that the issue is with the Cloud frontend; if you copy-as-image the graphics output the coloring is correct. $\endgroup$
    – kglr
    Oct 12, 2019 at 8:21
  • $\begingroup$ Thanks for the answers, I'll report this to Wolfram Support. $\endgroup$
    – banone
    Oct 14, 2019 at 6:25

1 Answer 1

4
$\begingroup$

Update: Yet another work-around:

cf = If[# > 0, Red, Green] &;
data = MapIndexed[{#2[[1]], #} &, Range[-10, 10]];

ListPlot[List /@ data,  PlotMarkers -> {"*", 24}, PlotStyle -> (cf /@ data[[All, 2]])]

enter image description here

Original answer:

Another work-around: Use ListPlot without the option PlotMarkers and post-process the output to replace Points with the desired marker:

 ClearAll[replaceMarkers]
 replaceMarkers[marker_] := Module[{colors = 
    Cases[#, HoldPattern[VertexColors -> c_List] :> (RGBColor @@@ c), All][[1]]}, 
   # /. Point[x_, ___] :> (Inset[Style[marker, colors[[#]]], #] & /@ x)] &;

Example:

lp0 = ListPlot[Range[-10, 10], 
   ColorFunction -> (If[#2 > 0, Red, Green] &), 
   ColorFunctionScaling -> False]

enter image description here

replaceMarkers[Style["*", 24]]@lp0

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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