8
$\begingroup$

Some stylistic attributes of plots are independent of colour, e.g. thickness, dashing, etc.

I can make the lines thick in this plot without having to specify a separate style for each line:

Plot[Table[BesselJ[n, x], {n, 4}] // Evaluate, {x, 0, 10}, 
 PlotStyle -> Thickness[0.01]]

enter image description here

I can also easily change the colour scheme:

Plot[Table[BesselJ[n, x], {n, 4}] // Evaluate, {x, 0, 10}, 
 PlotStyle -> 98]

enter image description here

How can I change both the colour scheme and the line thickness (or dashing) at the same time, without having to specify a separate style for each plotted function?

The following works, but it is cumbersome and requires hard-coding the number of plotted functions (or putting in an arbitrary large number):

Plot[Table[BesselJ[n, x], {n, 4}] // Evaluate, {x, 0, 10}, 
 PlotStyle -> (Directive[ColorData[98][#], Thickness[0.01]] & /@ Range[4])]

enter image description here

I know that I can do PlotStyle -> 98, PlotTheme -> "ThickLines", but that requires me to use a single pre-defined line thickness (or to choose from only a few). It does not allow me to set my own custom base plotting style with an arbitrary thickness and dashing specification.

$\endgroup$

1 Answer 1

9
$\begingroup$

You can use the (undocumented) Method suboption "DefaultPlotStyle" as a second place to specify plot styles:

Plot[Table[BesselJ[n, x], {n, 4}] // Evaluate, {x, 0, 10},
 PlotStyle -> 98,
 Method -> "DefaultPlotStyle" -> Thick
 ]

enter image description here

More generally, you could specify a list of directives in PlotStyle, and a common default in "DefaultPlotStyle":

Plot[Table[BesselJ[n, x], {n, 4}] // Evaluate, {x, 0, 10},
 PlotStyle -> {Directive[Red, Dashed], Green, Blue, 
   Directive[Orange, DotDashed]}, 
 Method -> "DefaultPlotStyle" -> Thick,
 BaseStyle -> Dotted
 ]

enter image description here

Note however that this can interfere with PlotTheme, since themes use the same option to set common default styles. In case more control is needed, you can register a custom plot theme that sets the "DefaultColor", "DefaultThickness", etc. suboptions before they are merged into "DefaultPlotStyle" by Charting`ResolvePlotTheme:

System`PlotThemeDump`resolvePlotTheme[{"CustomTheme", 
   opts : OptionsPattern[]}, s_] := Themes`SetWeight[
  {opts},
  Themes`$DesignWeight
  ]

This defines a new PlotTheme that accepts an arbitrary lists of options to be added to the plot theme options (see e.g. this answer for more details on the mechanism). It can be used for example like this:

Plot[TriangleWave[x], {x, 0, 3},
 PlotTheme -> {
   "CustomTheme",
   "DefaultColor" -> Red,
   "DefaultCurveStyle" -> JoinForm@"Round"
   }]

enter image description here

While you can in principle set any option with this "theme", the following are particularly interesting: "DefaultColor", "DefaultThickness", "DefaultTransparency", "DefaultDashing", "DefaultCurveStyle". This is because these particular options are combined into "DefaultPlotTheme" as part of Charting`ResolvePlotTheme, as mentioned above, which reduces interference between these custom settings and other themes (including the default one) as much as possible.

$\endgroup$
7
  • $\begingroup$ What does the 98 in PlotStyle -> 98 mean? Are such numeric values for PlotStyle documented somewhere? $\endgroup$
    – murray
    Feb 14, 2020 at 15:14
  • 1
    $\begingroup$ @murray The 98 refers to the indexed colour schemes. ColorData[98] should provide the same result. You can see the numbers for the colour schemes if you go into the "Palettes" menu > "Color Schemes". If you look at the "Indexed" palette, there are numbers associated with each one. $\endgroup$
    – MassDefect
    Feb 14, 2020 at 16:45
  • $\begingroup$ @murray 97 is the current default. The ones started at 97 were added in version 10.0 and I find them much more usable (distinguishable) than the earlier ones. $\endgroup$
    – Szabolcs
    Mar 2, 2020 at 12:33
  • $\begingroup$ @Lukas Thanks for pointing out the interference with the plot theme. It even interferes with the default style (e.g. it resets the thickness). But it is still very useful. $\endgroup$
    – Szabolcs
    Mar 2, 2020 at 12:33
  • $\begingroup$ @Szabolcs Can you elaborate on what you mean by "it resets the thickness"? Isn't the changed thickness the intended result? $\endgroup$
    – Lukas Lang
    Mar 2, 2020 at 13:08

Your Answer

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

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