16
$\begingroup$

This can be useful if the curve is passing over both dark and light backgrounds (like well-done subtitles in movies).

$\endgroup$
2

5 Answers 5

25
$\begingroup$

You could plot the curve twice, with two different styles:

Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, 
     PlotStyle -> {Directive[Thickness[0.03], White], Black}]

enter image description here

Changing the background to gray:

Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, 
     PlotStyle -> {Directive[Thickness[0.03], White], Black}, 
     Background -> Gray]

enter image description here

$\endgroup$
5
  • 4
    $\begingroup$ +1. Btw, when you add plots as images, you should try to export png-files from Mathematica because they don't suffer from the ringing artefacts at line borders. $\endgroup$
    – halirutan
    Commented May 4, 2012 at 10:45
  • $\begingroup$ @halirutan, Thanks for the tip. In this case I don't see any differences between jpeg and png though. I will stick to png in the future. $\endgroup$ Commented May 4, 2012 at 10:47
  • $\begingroup$ Fixed it. Really close to hard lines or sharp dark/bright changes you see clutter. $\endgroup$
    – halirutan
    Commented May 4, 2012 at 10:49
  • $\begingroup$ @halirutan: Thanks! $\endgroup$ Commented May 4, 2012 at 10:51
  • 2
    $\begingroup$ @MarkusRoellig, maybe add CapForm["Round"] to your Directive for a tidier appearance. $\endgroup$ Commented May 4, 2012 at 13:02
22
$\begingroup$

Here is another approach, based on Filling option :

Plot[{Sin[x] - 0.02, Sin[x], Sin[x] + 0.02}, {x, 0, 2 Pi}, 
      PlotStyle -> {Gray, Black, Gray}, 
      Filling -> {1 -> {{3}, Yellow}}]

enter image description here

One problem may appear here, namely if a given function has a big absolute value of the derivative, then the strip becomes too thin. We can avoid this by adding a factor depending on the absolute value of its derivative, e.g.

Plot[{Sin[x]-0.035(1 + Abs[Sin'[x]]), Sin[x], Sin[x] + 0.035 (1 + Abs[Sin'[x]])}, {x, 0, 2 Pi}, 
     PlotStyle -> {Gray, Black, Gray},  Filling -> {1 -> {{3}, LightOrange}}]

enter image description here

For a more customized solution we can use also one of the wide range of ColorSchemes :

Plot[{Sin[x]-0.035 (1+ Abs[Sin'[x]]), Sin[x], Sin[x] +0.035 (1+ Abs[Sin'[x]])}, {x, 0, 2 Pi}, 
      PlotStyle -> {Thin, Thick, Thin},  ColorFunction -> "FruitPunchColors",  
      FillingStyle -> Opacity[0.1],  Filling -> {1 -> {3}}]

enter image description here

GraphicsRow[ 
    Table[  Plot[{Sin[x]- 0.035 (1+ Abs[Sin'[x]]), Sin[x], Sin[x]+ 0.035 (1+ Abs[Sin'[x]])},
                 {x, 0, 2 Pi}, PlotStyle -> {Thin, Thick, Thin}, 
                 ColorFunction -> i, FillingStyle -> Opacity[0.05], Filling -> {1 -> {3}}], 
          {i, {"Rainbow", "BlueGreenYellow", "DeepSeaColors"}}]]

enter image description here

$\endgroup$
15
$\begingroup$

A minor tweak to Markus' method, you may find utility in CapForm:

Table[
  Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, 
   PlotStyle ->
     {Directive[AbsoluteThickness[15], CapForm[c], White], 
      Directive[AbsoluteThickness[7], Blue]},
   ImageSize -> 400, PlotRangePadding -> 0.2, Frame -> True,
   Prolog -> 
    Inset[ExampleData[{"TestImage", "Sailboat"}], 
        Scaled@{0.5, 0.5}, Automatic, Scaled@{1, 2}]
   ],
  {c, {"Butt", "Round", "Square"}}
] // Column

Mathematica graphics

Mathematica graphics

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ +1 for the CapForm and the Background :) nice! $\endgroup$ Commented May 4, 2012 at 20:10
4
$\begingroup$

A minor variation of Markus' method, applying the effect as a post process using ReplaceAll. Every Line in the graphics is replaced with two Lines: a thicker copy and the original. The key thing is to localise the additional directives to the extra Line by wrapping it in a list, so that subsequent primitives do not pick up the thickness and colour changes.

For plots with many curves this might be a more convenient approach than explicitly duplicating the plot functions.

Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}, PlotStyle -> {Yellow, White}, BaseStyle -> Thick] /. 
 l_Line :> {{Thickness[0.01], Black, CapForm["Round"], l}, l}

enter image description here

$\endgroup$
3
$\begingroup$

Since V 13.3 there is Haloing:

Plot[Sin[x], {x, 0, 2 Pi},
 PlotStyle -> Haloing[White, 0, 8],
 PlotTheme -> "Marketing"]

enter image description here

$\endgroup$

Your Answer

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

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