2
$\begingroup$

I have a 1D graph plotted using a certain set of colors and plotstyle. Is it possible for me to change the color(s) and the plotstyle without having to rerun the code.

$\endgroup$
3
  • 2
    $\begingroup$ No, not in any reasonably simple way. You'd have to decompose the Graphics object and figure out what to change in it. Then you have to do the change manually. $\endgroup$
    – Szabolcs
    Commented Aug 19, 2016 at 17:44
  • 2
    $\begingroup$ Can you give an example? It can be quite easy for simple ones: Plot[{Sin[x], Cos[x]}, {x, 0, 10}] /. {ColorData[97][1] -> Directive[Red, Dashed]} $\endgroup$
    – Karsten7
    Commented Aug 19, 2016 at 17:53
  • 1
    $\begingroup$ Not sure what a 1D graph is, and not sure which code you don't want to rerun. If this is a list plot, you should always generate your data separately from the plot. Do you really have such an expensive plot to generate, given the data? $\endgroup$
    – Alan
    Commented Aug 19, 2016 at 18:40

1 Answer 1

3
$\begingroup$

Some tips to get you started with a real simple example.

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 2 π}]

Mathematica graphics

You can extract the colors via

Cases[plot, color_?ColorQ, Infinity]

Mathematica graphics

FullForm[%]

Mathematica graphics

Replace the colors using Replace

plot /. {RGBColor[0.368417`, 0.506779`, 0.709798`] -> Red, 
         RGBColor[0.880722`, 0.611041`, 0.142051`] -> Black}

Mathematica graphics

If you want to replace more complicated items you will need to dig down into the graphical output of plot. Typically there is a great deal of numerical data that swamps the screen if you look at the graphic in text form.

To reduce that and try to locate what you might want to edit try:

plot /. Graphics -> graphicHead /. {x_?NumericQ, y_?NumericQ} -> Nothing

Nothing is relatively new (started in 10.2). graphicsHead is a bogus Head that prevents the plot from displaying.

The output looks like

Mathematica graphics

which would be helpful in locating the parts that you might want to edit.

$\endgroup$
4
  • $\begingroup$ Nothing was introduced in 10.2 according to WolframLanguageData["Nothing", "VersionIntroduced"]. You could use ColorQ to get all colors. Your version misses GrayLevel[0.5, 0.4]. $\endgroup$
    – Karsten7
    Commented Aug 20, 2016 at 0:22
  • $\begingroup$ @Karsten7 Thank you for the ColorQ tip, always learning something on StackExchange. Modified the answer and also corrected version where Nothing began. $\endgroup$ Commented Aug 20, 2016 at 13:59
  • $\begingroup$ @JackLaVigne You can consider my shortInputForm function which is very handy for investigation of inner structure of graphics. I have it in my init.m. $\endgroup$ Commented Aug 21, 2016 at 7:49
  • $\begingroup$ @AlexeyPopkov Thank you for sharing this with me. It works great!! $\endgroup$ Commented Aug 21, 2016 at 14:00

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