4
$\begingroup$

I want to make a plot, then check how it looks in GrayScale. If I use PlotLegends->Automatic, this isn't an issue:

Plot[x, {x, 0, 1}, PlotLegends -> Automatic]
ColorConvert[%, "Grayscale"]

enter image description here enter image description here

But if I try to specify PlotLegends then ColorConvert throws an error:

Plot[x, {x, 0, 1}, PlotLegends -> {"myplot"}]
ColorConvert[%, "Grayscale"]

>> [Appropriate first plot]
>> Rule::argr: Rule called with 1 argument; 2 arguments are expected.

In my use case, PlotLegends -> Automatic isn't possible (without tons of extra work), so this is a problem.

What's going on?

$\endgroup$
  • 2
    $\begingroup$ what happens if you try something like plot /. x_RGBColor:>ColorConvert[x,"Grayscale"] $\endgroup$ – Mike Honeychurch Apr 24 '17 at 2:28
  • $\begingroup$ That does the trick! Can you write an answer explaining what you did there? (I'm not familiar with the syntax) $\endgroup$ – Max Apr 24 '17 at 5:49
  • 1
    $\begingroup$ I get an additional error that says 'ColorConvert:[your plot is shown] should be a valid image, a color directive, a list of machine-sized real numbers of length up to 5, or a list of such objects.` If you apply FullForm to your plots: with automatic legend you get a graphics object, and the explicitly legended plot is wrapped in the head Legended. My guess is ColorConvert doesn't recognize Legended as a valid image. Mike is using the replacement rule to apply ColorConvert to RGB tuples directly, which ignores the Legended head. $\endgroup$ – N.J.Evans Apr 24 '17 at 12:45
4
$\begingroup$

For example

Plot[x, {x, 0, 1}, PlotLegends -> {"myplot"}] /. x_RGBColor:>ColorConvert[x,"Grayscale"]

which looks for heads with RGBColor and applies the colour conversion. Colours can also be GrayScale and Hue so more generally search for all colours (@kglr)

Plot[x, {x, 0, 1}, PlotLegends -> {"myplot"}] /. x_?ColorQ:>ColorConvert[x,"Grayscale"]
$\endgroup$
  • $\begingroup$ Can you maybe explain what that syntax is doing that's different from what I tried? $\endgroup$ – Max Apr 24 '17 at 6:51
  • 2
    $\begingroup$ (+1) more generally, x_?ColorQ instead of x_RGBColor. $\endgroup$ – kglr Apr 24 '17 at 18:15

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.