Oh, Matplotlib Christmas Tree!

Laura Richter
3 min readDec 30, 2022

--

Christmas trees by craiyon

Here I offer my own small contribution to the tradition of computer generated Christmas tree pictures[1].

We’ll use my favourite Matplotlib:

import matplotlib.pylab as plt

Now let’s set up a figure, set up some size scales to use, and start with the tree stump. We’ll use Matplotlib symbols to make our tree. Here, a square for a tree stump:

fig, ax = plt.subplots(1,1,figsize=(5,5))

d = 1e-2
s = 0.25e2

ax.plot(1, 1, marker='s', markersize=s, color='brown')

ax.set_ylim([0.99, 1.04])
ax.set_xlim([0.98, 1.02])

Voila! A tree stump:

I used Matplotlib’s NC notation to pick colors[2] from the default property cycle[3]. It’s a handy way to choose the default green, or default blue, etc. if you know that is what you want. The strings g, b, etc. don’t actually give the green, blue etc. of the default color cycle.

Next up, some leaves:

fig, ax = plt.subplots(1,1,figsize=(5,5))

d = 1e-2
s = 0.25e2

ax.plot(1, 1, marker='s', markersize=s, color='C5')

ax.plot(1, 1+2*d, marker='^', markersize=2*s, color='C2')
ax.plot(1, 1+1.5*d, marker='^', markersize=2.8*s, color='C2')
ax.plot(1, 1+d, marker='^', markersize=3.5*s, color='C2')

ax.set_ylim([0.99, 1.04])
ax.set_xlim([0.98, 1.02])

A star for the top!

fig, ax = plt.subplots(1,1,figsize=(5,5))

d = 1e-2
s = 0.25e2

ax.plot(1, 1, marker='s', markersize=s, color='brown')

ax.plot(1, 1+2*d, marker='^', markersize=2*s, color='C2')
ax.plot(1, 1+1.5*d, marker='^', markersize=2.8*s, color='C2')
ax.plot(1, 1+d, marker='^', markersize=3.5*s, color='C2')

ax.plot(1, 1+2.5*d, marker='*', markersize=s, color='C1')

ax.set_ylim([0.99, 1.04])
ax.set_xlim([0.98, 1.02])

And the final touch — some baubles!

fig, ax = plt.subplots(1,1,figsize=(5,5))

d = 1e-2
s = 0.25e2

ax.plot(1, 1, marker='s', markersize=s, color='brown')

ax.plot(1, 1+2*d, marker='^', markersize=2*s, color='C2')
ax.plot(1, 1+1.5*d, marker='^', markersize=2.8*s, color='C2')
ax.plot(1, 1+d, marker='^', markersize=3.5*s, color='C2')

ax.plot(1, 1+2.5*d, marker='*', markersize=s, color='C1')

baubles = [
[0.4, 0.5, 0.3*s],
[0.1, 0.35, 0.2*s],
[-0.4, 0.4, 0.3*s],
[-0.2, 0.55, 0.2*s],
[0.15, 0.65, 0.3*s],
[-0.05, 0.85, 0.2*s],
[-0.3, 0.95, 0.2*s],
[-0.2, 1.3, 0.3*s],
[0.3, 1.0, 0.3*s],
[0.05, 1.1, 0.3*s],
[0.1, 1.4, 0.2*s],
[0.15, 1.75, 0.2*s],
[-0.15, 1.7, 0.3*s],
[0, 2.0, 0.3*s],
]

for dx, dy, s in baubles:
ax.plot(1+dx*d, 1+dy*d, marker='o', markersize=s, color='C3')

ax.set_ylim([0.99, 1.04])
ax.set_xlim([0.98, 1.02])

There you have it, a Matplotlib marker Christmas tree!

Getting the dimensions and placing right took quite some fiddling. The baubles were a labour of love. I would not recommend this as a way to do graphic art. But we got a pretty neat Christmas tree out of the fiddling!

[1] For example, this scalable Christmas tree challenge.
[2] I know, it should be colour, not color, but Matplotlib uses Americanese and too many times I’ve been bitten by accidentally using colour in my code.
[3] Here is some info about the CN color spec, and the current colors used in the default property cycle.

--

--

No responses yet