2
$\begingroup$

I have let Mathematica to draw a plot of some functions written as below:

fplot = Plot[{Sinh[1/t],
   4 Cosh[1/(4 t)],-2 (Sinh[0.5/(2 t)]), -2 (Sinh[1/(2 t)] + Sinh[1/t])}, {t, 0.01, 1.5},

PlotStyle -> {{Thickness[0.008]}, {Thickness[0.008]}, {Thickness[
  0.008]}, {Thickness[0.008]}}
, PlotRange -> {-10, 10}, Frame -> True]

enter image description here

I tried fplot // Options but I cannot access the RGB codes of colors in the plot. How can I extract the RGB codes which Mathematica has by default used in this plot?

$\endgroup$
  • 1
    $\begingroup$ The plotting functions are countable and even finite in MMA, so a more viable approach is to just know, what is used. For the normal Plot function the colors are given by ColorData[97, "ColorList"] $\endgroup$ – LLlAMnYP Apr 28 '16 at 11:21
7
$\begingroup$

The following is a universal solution which extracts RGB color values assigned to the Line primitives of a plot generated by built-in plotting functions of Mathematica 10:

Cases[fplot, {___, c_Directive, __Line} :> 
   ColorConvert[c, RGBColor], Infinity] // InputForm
{RGBColor[0.368417, 0.506779, 0.709798, 1.], RGBColor[0.880722, 0.611041, 0.142051, 1.], 
 RGBColor[0.560181, 0.691569, 0.194885, 1.], RGBColor[0.922526, 0.385626, 0.209179, 1.]}

The above pattern is found by inspection of internal structure of Graphics objects produced by such functions as Plot, ListLinePlot, ParametricPlot, ContourPlot etc. of Mathematica 10.4.1. It is tested with plots generated by all listed functions and works quite reliably. If you encounter a case when it fails, please let me know.

For previous Mathematica versions more involved approach based on the colorQ function of Mr.Wizard is necessary. For example, for version 8.0.4 the following should be sufficiently general (but in many situations it will lose Opacity specification):

colorQ = Quiet@Check[Blend@{#, Red}; True, False] &;
Cases[fplot, {Longest[___], c_?colorQ, ___, __Line} :> ColorConvert[
   If[Head[c] =!= Directive, c, Last@Cases[c, _?colorQ, Infinity]]
   , RGBColor], Infinity]
{RGBColor[0.2472, 0.24, 0.6], RGBColor[0.6, 0.24, 0.442893], 
 RGBColor[0.6, 0.547014, 0.24], RGBColor[0.24, 0.6, 0.33692]}

This solution should work the same way with versions 9 and 10. The problem with Opacity is difficult to avoid because in version 8 ColorConvert cannot work with Directive.


For inspection of the structure of Graphics I recommend my shortInputForm function:

screenshot

An advanced description of the Mathematica graphical programming language can be found in these threads:


If you wish to find the default colors used by the built-in plotting functions, you should consult with the following threads:

$\endgroup$
  • $\begingroup$ Thanks so much for your answer and your explanation $\endgroup$ – Unbelievable Apr 28 '16 at 16:50
3
$\begingroup$
Cases[fplot, RGBColor[x_, y_, z_] :> {x, y, z}, Infinity]

{{0.368417, 0.506779, 0.709798}, {0.880722, 0.611041, 0.142051}, {0.560181, 0.691569, 0.194885}, {0.922526, 0.385626, 0.209179}}

$\endgroup$
  • $\begingroup$ Normal does nothing in your solution (it doesn't affect colors in any way). And your solution will fail if the color is specified not as RGBColor but as GrayLevel, Hue, XYZColor or other color model. In many situations it will also return nonworking colors which are present on the low level but do not affect the display. $\endgroup$ – Alexey Popkov Apr 28 '16 at 11:14
  • $\begingroup$ Thanks @AlexeyPopkov. I didn't actually notice that Normal is still there. I agree with you about the other colors. It just a old fashioned way I use and wanted to share :) $\endgroup$ – Sumit Apr 28 '16 at 12:11

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.