Monday 9 May 2016

Visually engaging periodic plots using Python

In order to obtain periodic images with a technical feel to be embedded in a website, I decided to compose them with Python and Matplotlib. The plots are essentially some sums and multiplications of "noisy" sines.



The Python script outputs a SVG image, which can be later converted to the desired format. Eventually, here I converted the plots to png using a simple bash script to be run inside the svg folder:
for i in *.svg; do inkscape --export-png=$i.png --export-dpi=300 --export-background-opacity=0 --without-gui $i; done
optipng *.png
The script utilizes Inkscape, which converts the svg to png, and optipng, which optimizes the final size of the images.
The png figures obtained have a transparent background, namely the alpha channel, by default. This can be changed in the script or graphically by using GIMP (or whatever). For example, if you would have a white background:
for i in *.svg; do inkscape --export-png=$i.png --export-dpi=300 --export-background-opacity=255 --without-gui $i; done
optipng *.png

In the following, I will illustrate some code snippets and the corresponding results obtained.

First Plot:



Code:

import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 6
x = np.array([np.linspace(0, (2 * (periods) * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4) + 0.5 * n**2
f1 = plt.figure("GOOD1")
ax = f1.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f1.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
f1.savefig("GOOD1.svg", bbox_inches=0, transparent=True)


Note:

The transparency for each line is set to alpha=(0.015625): in the process of selecting that value, particular attention should be devoted to the transparency channel which is 8 bit wide, and hence it has to be a multiple of 1/255.



Second plot:




import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 6
x = np.array([np.linspace(0, (2 * (periods) * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4)
f2 = plt.figure("GOOD2")
ax = f2.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f2.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD2.svg", bbox_inches=0, transparent=True)


Third plot:



import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 6
x = np.array([np.linspace(0, (2 * (periods) * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4) + 0.5 * np.abs(n)
f3 = plt.figure("GOOD3")
ax = f3.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f3.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD3.svg", bbox_inches=0, transparent=True)


Fourth plot:



import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 2048
periods = 3
x = np.array([np.linspace(- 0.5 * np.pi,
                          (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4) + 0.5 * x * np.abs(n)
x = np.array([np.linspace(0, 2 * (2 * (periods) *
                 np.pi - 0.5 * np.pi), 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f4 = plt.figure("GOOD4")
ax = f4.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f4.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD4.svg", bbox_inches=0, transparent=True)


Fifth plot:





import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 3
x = np.array([np.linspace(0, (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n) + 4) + 3 * np.abs(n) * (1 - np.sin(x) * np.abs(n))
x = np.array([np.linspace(0, 2 * (2 * (periods) *
                 np.pi - 0.5 * np.pi), 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f5 = plt.figure("GOOD5")
ax = f5.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f5.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD5.svg", bbox_inches=0, transparent=True)


Sixth plot:



import numpy as np
import matplotlib.pyplot as plt
plt.close("all")


i = 1000
j = 2048
periods = 3
x = np.array([np.linspace(0, (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n) + 4) + 3 * np.abs(n) * (1 - np.sin(x))
x = np.array([np.linspace(0, 2 * (periods) * np.pi - 0.5 * np.pi, 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f6 = plt.figure("GOOD6")
ax = f6.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f6.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD6.svg", bbox_inches=0, transparent=True)


Seventh plot:



import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 3
x = np.array([np.linspace(0, (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = (np.sin(x)*(np.sin(n)+4) + 3*np.abs(n)*(1-np.abs(np.sin(x))))
x = np.array([np.linspace(0, 2 * (2 * (periods) *
                 np.pi - 0.5 * np.pi), 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f7 = plt.figure("GOOD7")
ax = f7.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f7.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD7.svg", bbox_inches=0, transparent=True)


Eighth plot:



import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 3
x = np.array([np.linspace(0, (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = (np.sin(x) * (np.sin(n) + 4) + 3 * np.abs(np.sin(n)) -
     1 * (x-(2 * (periods) * np.pi - 0.5 * np.pi) * 0.5) *
     np.abs((1 - 0.5 * np.sin(x)) *
     np.concatenate((np.sin(n[::2]), np.sin(n[::2])))))
x = np.array([np.linspace(0, 2 * (2 * (periods) *
                 np.pi - 0.5 * np.pi), 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f8 = plt.figure("GOOD8")
ax = f8.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f8.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD8.svg", bbox_inches=0, transparent=True)


Ninth plot:




import numpy as np
import matplotlib.pyplot as plt
plt.close("all")

i = 1000
j = 1024
periods = 3
x = np.array([np.linspace(0, (2 * (periods) * np.pi - 0.5 * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
     np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = (np.sin(x) * (np.sin(n) + 4) + 3 * np.abs(np.sin(n)) -
     1 * (x-(2 * (periods) * np.pi - 0.5 * np.pi) * 0.5) *
     np.abs((1 - 0.5 * np.sin(x)) *
     np.concatenate((np.sin(n[::2]), np.sin(n[::2])))) +
     np.sin(x) * (np.sin(n) + 4) + 3 * np.abs(n) * (1 - np.sin(x)))
x = np.array([np.linspace(0, 2 * (2 * (periods) *
                 np.pi - 0.5 * np.pi), 2 * i)]).T
x = np.repeat(x, j, axis=1)
y = np.concatenate((y, y[::-1]))
f9 = plt.figure("GOOD9")
ax = f9.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f9.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
plt.savefig("GOOD9.svg", bbox_inches=0, transparent=True)

No comments:

Post a Comment