# XKCD style graphs?

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

The matlab and R version not quite so much, still for reference:

Any takers?

f

Hi Fernando,

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

···

Le 04/10/2012 09:16, Fernando Perez a écrit :

I believe this is in your interests: http://i.imgur.com/5XwRO.png

Here's the code: https://gist.github.com/3832579

Disclaimer: The code is ugly; don't judge me. Also, I installed the
Humor Sans font but I couldn't get mpl to find it. Oh well

···

On Thu, Oct 4, 2012 at 10:02 AM, Pierre Haessig <pierre.haessig@...1709...> wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a écrit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

I got the font working http://i.imgur.com/Dxemm.png

···

On Thu, Oct 4, 2012 at 10:44 AM, Damon McDougall <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:02 AM, Pierre Haessig > <pierre.haessig@...1709...> wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a écrit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

I believe this is in your interests: http://i.imgur.com/5XwRO.png

Here's the code: https://gist.github.com/3832579

Disclaimer: The code is ugly; don't judge me. Also, I installed the
Humor Sans font but I couldn't get mpl to find it. Oh well

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

Nice challenge Fernando!

Damon, I love the solution! I do wonder whether we could do some
quirky transform on the lines to achieve a similar result, rather than
manipulating the data before plotting it. The benefit is that
everything should then get randomly Xkcd-ed automatically - maybe I
will save that one for a rainy day....

Thanks for posting!

···

On 4 October 2012 11:31, Damon McDougall <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:44 AM, Damon McDougall > <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:02 AM, Pierre Haessig >> <pierre.haessig@...1709...> wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a écrit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

I believe this is in your interests: http://i.imgur.com/5XwRO.png

Here's the code: https://gist.github.com/3832579

Disclaimer: The code is ugly; don't judge me. Also, I installed the
Humor Sans font but I couldn't get mpl to find it. Oh well

I got the font working http://i.imgur.com/Dxemm.png

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

f@...4223... means f(r)

a~ImageConvolve~b means ImageConvolve(a,b) (~ treats an operator as infix)

Table[..., {2}] means [... for i in range(2)]

#+1& is a lambda function lambda x: x+1

So I think it goes something like:

def xkcdDistort(p):
(ix, iy) = [ImageConvolve(RandomImage([-1,1], ImageDimensions(r)),
GaussianMatrix(10))
for i in range(2)]
lambda coord: (coord[0]+15*ImageValue(ix, coord),
coord[1]+15*ImageValue(iy, coord)),
DataRange='Full'),
-5)

Thanks,

Jason

···

On 10/4/12 4:02 AM, Pierre Haessig wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a écrit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

Yes -- this would be a great application for the path filtering infrastructure that matplotlib has.

Mike

···

On 10/04/2012 08:29 AM, Phil Elson wrote:

Nice challenge Fernando!

Damon, I love the solution! I do wonder whether we could do some
quirky transform on the lines to achieve a similar result, rather than
manipulating the data before plotting it. The benefit is that
everything should then get randomly Xkcd-ed automatically - maybe I
will save that one for a rainy day....

Thanks for posting!

On 4 October 2012 11:31, Damon McDougall <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:44 AM, Damon McDougall >> <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:02 AM, Pierre Haessig >>> <pierre.haessig@...1709...> wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a �crit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

I believe this is in your interests: http://i.imgur.com/5XwRO.png

Here's the code: https://gist.github.com/3832579

Disclaimer: The code is ugly; don't judge me. Also, I installed the
Humor Sans font but I couldn't get mpl to find it. Oh well

I got the font working http://i.imgur.com/Dxemm.png

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

A different solution to get the shaken effect on every graphic items is
the post-processing of a raster rendering of the plot. I think this is
what was proposed with Mathematica though I'm really unfamiliar with its
syntax

One way I see to "shake" on image would be to use
scipy.ndimage.interpolation.map_coordinates [1] to interpolate the
rastered plot image with a "shaken grid". This shaken grid would be a
regular 2D indexing grid + some 2D noise, carefully tuned to have a bit
of spatial correlation.
I'm not so familiar with image processing in Python though, so there may
be better solutions I'm not aware of.

Best,
Pierre

···

Le 04/10/2012 14:29, Phil Elson a écrit :

Damon, I love the solution! I do wonder whether we could do some
quirky transform on the lines to achieve a similar result, rather than
manipulating the data before plotting it. The benefit is that
everything should then get randomly Xkcd-ed automatically - maybe I
will save that one for a rainy day....

This is just too cool of an idea to pass up -- I'm going to see if I can put together a PR that does this using the C++ path filtering stuff so it would be available everywhere.

Mike

···

On 10/04/2012 10:11 AM, Michael Droettboom wrote:

Yes -- this would be a great application for the path filtering
infrastructure that matplotlib has.

Mike

On 10/04/2012 08:29 AM, Phil Elson wrote:

Nice challenge Fernando!

Damon, I love the solution! I do wonder whether we could do some
quirky transform on the lines to achieve a similar result, rather than
manipulating the data before plotting it. The benefit is that
everything should then get randomly Xkcd-ed automatically - maybe I
will save that one for a rainy day....

Thanks for posting!

On 4 October 2012 11:31, Damon McDougall <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:44 AM, Damon McDougall >>> <damon.mcdougall@...287...> wrote:

On Thu, Oct 4, 2012 at 10:02 AM, Pierre Haessig >>>> <pierre.haessig@...1709...> wrote:

Hi Fernando,

Le 04/10/2012 09:16, Fernando Perez a �crit :

This would make for an awesome couple of examples for the gallery, the
mathematica solutions look really pretty cool:

http://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs

I've never used Mathematica so that it's pretty difficult for me to
understand the following lines of code which I guess do the main job of
distorting the image

xkcdDistort[p_] := Module[{r, ix, iy},
{ix, iy} =
Table[RandomImage[{-1, 1}, ImageDimensions@...4223...]~ImageConvolve~
GaussianMatrix[10], {2}];
# + 15 {ImageValue[ix, #], ImageValue[iy, #]} &, DataRange ->
Full], -5]];

Is there somebody there that can describe this algorithm with words
(English or Python ;-)) ?

I feel like the key point is about adressing the rasterized plot image
"r" with some slightly randomized indices "ix" and "iy". However, I
really don't get the step that generates these indices.

Best,
Pierre

I believe this is in your interests: http://i.imgur.com/5XwRO.png

Here's the code: https://gist.github.com/3832579

Disclaimer: The code is ugly; don't judge me. Also, I installed the
Humor Sans font but I couldn't get mpl to find it. Oh well

I got the font working http://i.imgur.com/Dxemm.png

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I agree with this idea. However, I don’t think the code is set up to allow for user-defined path filters. Maybe an AGG filter would be sufficient in the short-term?

Ben Root

···

On Thu, Oct 4, 2012 at 10:11 AM, Michael Droettboom <mdroe@…86…> wrote:

Yes – this would be a great application for the path filtering

infrastructure that matplotlib has.

Mike

Thanks a lot!

It's the first time I encounter Mathematica syntax. Some of these
functional notations are not so easy to follow for my unexperienced eyes
but it makes this Mathematica code nicely compact.

So I think this code indeed resamples the rastered plot image on a
shaken coordinate grid. I kind of understand that the noise on
coordinates is spatially smoothed by a 10px Gaussian Point Spread
Function (if I understand correctly...)

Best,
Pierre

···

Le 04/10/2012 16:03, Jason Grout a écrit :

f@...4223... means f(r)

a~ImageConvolve~b means ImageConvolve(a,b) (~ treats an operator as infix)

Table[..., {2}] means [... for i in range(2)]

#+1& is a lambda function lambda x: x+1

So I think it goes something like:

def xkcdDistort(p):
(ix, iy) = [ImageConvolve(RandomImage([-1,1], ImageDimensions(r)),
GaussianMatrix(10))
for i in range(2)]
lambda coord: (coord[0]+15*ImageValue(ix, coord),
coord[1]+15*ImageValue(iy, coord)),
DataRange='Full'),
-5)

Sounds way cooler than post-processing a raster plot image !

I'm not aware of this path filtering infrastructure. I guess it's a
deeply buried facility which is not accessible in the "Python user space" ?

Best,
Pierre

···

Le 04/10/2012 16:11, Michael Droettboom a écrit :

Yes -- this would be a great application for the path filtering
infrastructure that matplotlib has.

Is that the same as the path effects features, like http://matplotlib.org/examples/pylab_examples/patheffect_demo.html ?

Thanks,

Jason

···

On 10/4/12 9:11 AM, Michael Droettboom wrote:

Yes -- this would be a great application for the path filtering
infrastructure that matplotlib has.

That is correct. In path.so, there are some functions that are explicitly called to do any cleanup and simplification on the paths. We would have to do some work to allow for user-defined functions. I once considered doing this back in the beginning of summer to address some contouring “bugs” I encountered, but found other, more simple solutions.

Cheers!
Ben Root

···

On Thu, Oct 4, 2012 at 10:39 AM, Pierre Haessig <pierre.haessig@…1709…> wrote:

Le 04/10/2012 16:11, Michael Droettboom a écrit :

Yes – this would be a great application for the path filtering

infrastructure that matplotlib has.

Sounds way cooler than post-processing a raster plot image !

I’m not aware of this path filtering infrastructure. I guess it’s a

deeply buried facility which is not accessible in the “Python user space” ?

Best,

Pierre

Slightly different. That is through the AGG layer, so vector-based backends wouldn’t benefit, IIRC. That being said, this is probably the better place to implement this (maybe this is what Mike was thinking of?).

Ben Root

···

On Thu, Oct 4, 2012 at 10:41 AM, Jason Grout <jason-sage@…2130…> wrote:

On 10/4/12 9:11 AM, Michael Droettboom wrote:

Yes – this would be a great application for the path filtering

infrastructure that matplotlib has.

Is that the same as the path effects features, like

Thanks,

Jason

Adding Gaussian noise to each point on a function doesn't look nice.
That's why I produced a random function in Fourier space first. That
way, random functions still have some sense of smoothness.

···

On Thu, Oct 4, 2012 at 3:35 PM, Pierre Haessig <pierre.haessig@...1709...> wrote:

Le 04/10/2012 16:03, Jason Grout a écrit :

f@...4223... means f(r)

a~ImageConvolve~b means ImageConvolve(a,b) (~ treats an operator as infix)

Table[..., {2}] means [... for i in range(2)]

#+1& is a lambda function lambda x: x+1

So I think it goes something like:

def xkcdDistort(p):
(ix, iy) = [ImageConvolve(RandomImage([-1,1], ImageDimensions(r)),
GaussianMatrix(10))
for i in range(2)]
lambda coord: (coord[0]+15*ImageValue(ix, coord),
coord[1]+15*ImageValue(iy, coord)),
DataRange='Full'),
-5)

Thanks a lot!

It's the first time I encounter Mathematica syntax. Some of these
functional notations are not so easy to follow for my unexperienced eyes
but it makes this Mathematica code nicely compact.

So I think this code indeed resamples the rastered plot image on a
shaken coordinate grid. I kind of understand that the noise on
coordinates is spatially smoothed by a 10px Gaussian Point Spread
Function (if I understand correctly...)

Best,
Pierre

--
Damon McDougall
http://www.damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

We have a complete set of path filters in C++ in path_converters.h
that are used by most of the backends. It’s not really user-defined
because it can’t be extended from Python, but it should be
sufficient to put it in there and have it work everywhere.
Mike

···

On 10/04/2012 10:29 AM, Benjamin Root
wrote:

``````    On Thu, Oct 4, 2012 at 10:11 AM, Michael
``````

Droettboom <mdroe@…86…>
wrote:

``````      Yes -- this would be a great application for the path
``````

filtering

``````      infrastructure that matplotlib has.

Mike
``````
``````      I agree with this idea.  However, I don't think the code is
``````

set up to allow for user-defined path filters. Maybe an AGG
filter would be sufficient in the short-term?

Mathematica code seems to use a Gaussian *smoothing* of a uniform noise.
I understand this as the spatial-domain-way (using convolution) to get
some smoothness while you've taken the frequency-domain path. It's a
matter of taste and I guess that both ways should be ok !

Best,
Pierre

···

Le 04/10/2012 16:54, Damon McDougall a écrit :

Adding Gaussian noise to each point on a function doesn't look nice.
That's why I produced a random function in Fourier space first. That
way, random functions still have some sense of smoothness.

I've implemented this processing in a tiny "image_shake" script.
https://gist.github.com/3834536
A nice occasion to learn how to use some scipy image processing functions...

I've attached the before/after images because I didn't manage to put
them in the Gist (it's not a plot image but gives the idea of line shaking).

Now, I think it's unfortunately outside the frame of Fernando's
challenge, because this script uses zero matplotlib methods!!

Best,
Pierre

···

Le 04/10/2012 16:35, Pierre Haessig a écrit :

So I think this code indeed resamples the rastered plot image on a
shaken coordinate grid. I kind of understand that the noise on
coordinates is spatially smoothed by a 10px Gaussian Point Spread
Function (if I understand correctly...)

Here is my take on it as an IPython notebook, based on Damon's code:
http://nbviewer.ipython.org/3835181/

I took the engineering approach and filtered the random function instead of doing some fft/ifft magic.
Also, X and Y of the functions are affected now, giving them a more "natural" look in the slopes.

Juergen

···

Am 04.10.2012 18:09, schrieb Pierre Haessig:

Le 04/10/2012 16:35, Pierre Haessig a �crit :

So I think this code indeed resamples the rastered plot image on a
shaken coordinate grid. I kind of understand that the noise on
coordinates is spatially smoothed by a 10px Gaussian Point Spread
Function (if I understand correctly...)

I've implemented this processing in a tiny "image_shake" script.
https://gist.github.com/3834536
A nice occasion to learn how to use some scipy image processing functions...

I've attached the before/after images because I didn't manage to put
them in the Gist (it's not a plot image but gives the idea of line shaking).

Now, I think it's unfortunately outside the frame of Fernando's
challenge, because this script uses zero matplotlib methods!!

Best,
Pierre