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.
1 Answer
Some tips to get you started with a real simple example.
plot = Plot[{Sin[x], Cos[x]}, {x, 0, 2 π}]
You can extract the colors via
Cases[plot, color_?ColorQ, Infinity]
FullForm[%]
Replace the colors using Replace
plot /. {RGBColor[0.368417`, 0.506779`, 0.709798`] -> Red,
RGBColor[0.880722`, 0.611041`, 0.142051`] -> Black}
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
which would be helpful in locating the parts that you might want to edit.
-
$\begingroup$
Nothing
was introduced in 10.2 according toWolframLanguageData["Nothing", "VersionIntroduced"]
. You could useColorQ
to get all colors. Your version missesGrayLevel[0.5, 0.4]
. $\endgroup$– Karsten7Commented 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 whereNothing
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 myinit.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
Graphics
object and figure out what to change in it. Then you have to do the change manually. $\endgroup$Plot[{Sin[x], Cos[x]}, {x, 0, 10}] /. {ColorData[97][1] -> Directive[Red, Dashed]}
$\endgroup$