Another way to approach the xkcd-ification of plots is from an image processing perspective. The idea is to warp the space in which the image lies rather than to try and warp the lines themselves. When the image-space warps, the lines appear to vary in thickness.

First define the following function, which is *nearly* just a line with slope one. The important part is that it has small sinusoidal oscillations about this slope. A function that does this is

```
f[x_, freq_, str_] := 0.99 x + Sin[(freq + 12 Sin[4 Pi x]) x]/str ;
```

which has two parameters: one controls the frequency of the oscillation and the other controls the strength/amount of the warping. To see how this function can be applied to the image space, start with a simple plot (from Mr. Wizard's "the race"). Since the lines are so thin, they need to be widened, which is done here using erosion. The function `f`

is applied to both the x and y directions (the pure functions `#[[1]]`

and `#[[2]]`

) using `ImageTransformation`

```
plot = Plot[{x/2, (x + Sin[x])/2.2}, {x, 0, 2 Pi},
Frame -> {True, True, False, False}, FrameTicks -> None]
ImageTransformation[Erosion[Image[plot], 1],
{f[#[[1]], 80, 500], f[#[[2]], 105, 500]} &]
```

If there are no thin lines, there is no need to do the erosion:

```
GraphicsRow[{piePlot = Image[PieChart[{9, 1}]],
ImageTransformation[piePlot, {f[#[[1]], 70, 180], f[#[[2]], 80, 180]} &]},
ImageSize -> 500]
```

Here's another example (taken from Mr. Wizard's answer) of this image transformation

```
GraphicsRow[{plot3 =Plot[{3 Sin@x, Cos@x, Tan[x]}, {x, 0, 2 Pi},
MaxRecursion -> 0, PlotPoints -> 30, PlotRange -> {-2, 2},
Frame -> {True, True, False, False}, FrameTicks -> None, Axes -> False],
ImageTransformation[ Erosion[Image[plot3], 1],
{f[#[[1]], 64, 300], f[#[[2]], 80, 400]} &]}, ImageSize -> 600]
```

Using a Manipulate, it is easy to explore a fairly wide variety of hand-drawn effects. Using the plot from above

```
Manipulate[
ImageTransformation[ Erosion[Image[plot],1],
{f[#[[1]], freq, m], g[#[[2]], freq + 10, m]} &],
{{freq, 40,"frequency"}, 0, 200}, {{m, 500, "strength"}, 100, 1000, 10}]
```

The same idea an also be applied to text

```
text = Style["Every font is comic sans", FontSize -> 50, FontFamily -> "Geneva"]
ImageTransformation[Image[Rasterize[text]],
{f[#[[1]], 64, 200], f[#[[2]], 90, 200]} &]
```

which has the interesting property that different occurrence of a letter will not be the same (because they are warped differently by the underlying space). In this example, notice how the three s's, two n's and c's differ from each other.

And finally (I promise to stop adding new examples) it can be applied to any image. Here is a pattern that shows how the underlying space is warped by the function `f`

:

```
GraphicsRow[{img2 = ColorNegate[Import["http://i.stack.imgur.com/F8Plt.png"]],
ImageTransformation[img2,{f[#[[1]], 90, 100], f[#[[2]], 80, 50]} &]},
ImageSize->500]
```

And here is a full StackExchange xkcdified plot using the above transformation. The bulk of the code handles the labels and coloring. The `Tooltip`

allows a secret mouse-over message, in the best xkcd tradition.

```
f[x_, freq_, str_] := 0.99 x + Sin[(freq + 12 Sin[4 Pi x]) x]/str;
fTicks = {{{{0.2, "hmm"}, {0.8, "wow!"}}, {{0.2, "boring"}, {0.8, "very\nboring"}}}, {{{0.2, "not enough"}, {0.8, "too much"}}, None}};
fLabels = {{Style["Today's StackExchange\nquestions", FontSize -> 13, Darker[Red]], Rotate[Style["Today's work", FontSize -> 13, Darker[Blue]], Pi]}, {Style["Time spent on Mathematica StackExchange", FontSize -> 13, Black], None}};
tip = Style["This seems to be a complex optimization problem.\nCan someone write the code for me?", FontFamily -> "Comic Sans MS", FontSize -> 13];
fTickStyle = {{Darker[Red], Darker[Blue]}, {Black, None}};
plot1 = Plot[{x^2, Exp[- 2 x]}, {x, 0, 1}, Axes -> False];
plot2 = Plot[None, {x, 0, 1}, PlotRange -> {0, 1}, Frame -> {{True, True}, {True, None}}, FrameTicks -> fTicks, FrameTicksStyle -> fTickStyle, LabelStyle -> Directive[FontFamily -> "Comic Sans MS"], FrameLabel -> fLabels];
xkcdified = ImageTransformation[ Erosion[Image[plot1], 2], {f[#[[1]], 80, 500], f[#[[2]], 105, 500]} &];
Tooltip[ImageCompose[ImageResize[Image[plot2], 600], ImageResize[xkcdified, 350],{Center, 210}], tip]
```

Analogous functionality in matplotlib. – Emilio Pisanty – 2014-08-29T09:50:43.580

Haven't been on Mathematica before so I don't have the rep for an answer, but if people are looking for interactive solutions then I've just discovered

– pospi – 2014-09-17T23:51:09.217amchartsand they have a 'hand drawn' option that gets you most of the way to this style. (ignore the blackboardesque theme, you can style black on white with CSS easily)1For the text floating around you might just use

`Text`

in combination with`Graphics`

instead of`PlotLegend`

. – VLC – 2012-10-01T07:47:36.650Making wiggly curves shouldn't be a problem say with splines; another issue is the font... you probably also need to interrupt curves when they intersect which implies you need to find the intersections. – chris – 2012-10-01T07:47:59.980

Another challenge is the obvious white gap in a curve where another curve crosses over it. Do you prefer to have it? – Vitaliy Kaurov – 2012-10-01T08:13:31.467

16@Vitaliy couldn't you just draw two lines, a thicker white one behind and the thinner colored one in front? – Mr.Wizard – 2012-10-01T08:14:50.340

@Mr.Wizard That's a grand idea ! – Vitaliy Kaurov – 2012-10-01T08:17:46.347

9

@chris, the font's already been taken care of... (see this as well.)

– J. M.'s ennui – 2012-10-01T08:51:41.840This does the wiggly curve...

`pts = Table[{x, 5*Sin[x]/x}, {x, 0.01, 10, 0.1}]; pts2 = pts + RandomReal[{-0.1, 0.1}/2, Dimensions[pts]]; f = BSplineFunction[pts2]; ParametricPlot[f[x], {x, 0.1, 0.9}, PlotStyle -> {Darker[Cyan, 0.3], AbsoluteThickness[3]}]`

– chris – 2012-10-01T09:09:02.3105

related: http://tex.stackexchange.com/q/74878/430

– Tobias Kienzler – 2012-10-01T18:26:37.5301

Another clone: http://stackoverflow.com/q/12675147

– rm -rf – 2012-10-01T18:47:47.02064To all the new users who have been attracted by this question, we encourage you to stick around and get to know what else Mathematica can do. – Verbeia – 2012-10-01T22:39:00.733

1

Latex users can use the pgf package with the 'random steps' or 'bent' line decorations, as already answered in this tex.stackexchange question.

– Jose M Vidal – 2012-10-01T20:39:24.8801Where did all the visitors come from? How did everyone hear about this challenge? – DavidC – 2012-10-01T22:49:23.857

2@DavidCarraher It quickly became a hot question and was featured on the SE hot questions page. I shared it on reddit, which caused the views and votes to explode – rm -rf – 2012-10-02T00:47:18.970

@DavidCarraher Also got to the top of hacker news. – s0rce – 2012-10-02T04:55:55.670

6

Now I am seriously thinking of using this for the Tech Conference Mathematica.SE promotion...

– Yves Klett – 2012-10-02T13:25:43.873@Amatya, you should send the question's link to Randall Munroe. – FredrikD – 2012-10-02T18:58:31.937

1@FredrikD I tweeted the link to him. – Amatya – 2012-10-02T21:17:23.837

1Matlab version of the question. – Jonas – 2012-10-03T19:02:39.937

2

This answer was highlighted as an exceptional in the Wolfram Blog - thank you for excellent contribution! http://blog.wolfram.com/2012/10/05/automating-xkcd-diagrams-transforming-serious-to-funny/

– Vitaliy Kaurov – 2012-10-05T18:47:28.7232

Mathematica9 users please see the follow-up post at http://mathematica.stackexchange.com/questions/17272/xkcdconvert-routines-perform-slower-in-mathematica-9 concerning slower speeds. – whuber – 2013-01-07T22:08:30.943@J. M. I notice you changed "Woods'" to "Woods's" -- I thought the former was the correct form. Does this rule differ geographically? – Mr.Wizard – 2013-06-20T00:38:09.180

@Mr.Wizard, the rule that I was accustomed to was that since "Woods" is Simon's fine surname, and manifestly not plural, the proper course for forming the possessive is "Woods's", and AFAICT this is not one of those exceptions of a singular noun whose possessive is formed by simply appending an apostrophe. There is a mention of this in Strunk and White, off the top of my head. – J. M.'s ennui – 2013-06-20T01:06:10.530

@J. M. Thanks for the education. :-) – Mr.Wizard – 2013-06-20T01:14:01.923