# Matplotlib 圖片、等值輪廓線

``````import matplotlib.pyplot as plt

plt.title('face')
plt.imshow([
[[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
[[255, 255, 255], [255,   0,   0], [255, 255, 255], [255,   0,   0], [255, 255, 255]],
[[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]],
[[255, 255, 255], [255,   0,   0], [255,   0,   0], [255,   0,   0], [255, 255, 255]],
[[255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255], [255, 255, 255]]
])
plt.show()
``````

``````import matplotlib.pyplot as plt
from matplotlib import cm

plt.title('face')
plt.imshow([
[255, 255, 255, 255, 255],
[255,   0, 255,   0, 255],
[255, 255, 255, 255, 255],
[255,   0,   0,   0, 255],
[255, 255, 255, 255, 255]
], cmap = cm.gray)
plt.show()
``````

``````from PIL import Image
import matplotlib.pyplot as plt

img = Image.open('caterpillar.jpg')

plt.title('caterpillar')
plt.imshow(img)
plt.show()
``````

``````from math import floor
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# Perlin noise 實作
def blending(t):
return 6 * (t ** 5) - 15 * (t ** 4) + 10 * (t ** 3)

def lerp(g1, g2, t):
return g1 + t * (g2 - g1)

return [dy, dx + dy, dx, dx - dy, -dy, -dx - dy, -dx, -dx + dy][hashvalue % 8];

rand_table = np.random.randint(255, size = 256).tolist()
def _perlin2(x, y):
xi = floor(x)
yi = floor(y)

aa = rand_table[
(rand_table[xi % 256] + yi) % 256
]
ba = rand_table[
(rand_table[(xi + 1) % 256] + yi) % 256
]
ab = rand_table[
(rand_table[xi % 256] + yi + 1) % 256
]
bb = rand_table[
(rand_table[(xi + 1) % 256] + yi + 1) % 256
]

dx = x - xi
dy = y - yi

u = blending(dx)
v = blending(dy)

g2 = lerp(grad2(ab, dx, dy - 1), grad2(bb, dx - 1, dy - 1), u)

return lerp(g1, g2, v)
_perlin2 = np.frompyfunc(_perlin2, 2, 1)

def perlin2(x, y):
cx, cy = np.meshgrid(x, y)
return _perlin2(cx, cy).astype(np.float)

# 將雜訊當成灰階度顯示為圖片

width = 500
x = np.arange(width) / 100
y = np.arange(width) / 100
noise = (perlin2(x, y) + 1) * 125

plt.title('Perlin noise')
plt.imshow(noise, cmap = cm.gray)
plt.show()
``````

``````plt.contour(noise)
``````

``````plt.contour(noise, levels = 25)
``````