使用Jupyter重新绘制matplotlib.pyplot.imshow

时间:2018-08-22 20:26:06

标签: python matplotlib jupyter-notebook

我试图绘制这些漂亮的框,但我希望每个框都覆盖前一个框,而不是垂直绘制它们,所以看起来就像一个框每隔0.5秒更改颜色

我正在使用Jupyter笔记本和Python 3.6。

我已经阅读了约50个类似的问题和答案,但无法按需显示。有人可以帮忙吗?

from PIL import Image
import random
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow, show, clf

sq_size = 20
num_squares = 5
im_size = sq_size * num_squares

im = Image.new('RGB', (im_size, im_size), color = 'white')

pix = im.load()

#Create new colourful boxes 4 times
for _ in range(4):

    startx, starty = 0, 0

    #move to the right place to plot each small square
    for i in range(num_squares):
        startx += sq_size
        for j in range(num_squares):
            starty += sq_size
            rshade = np.random.randint(0, 256)
            gshade = np.random.randint(0, 256)
            bshade = np.random.randint(0, 256)

            #plot each small square
            for x in range(sq_size):
                for y in range(sq_size):
                    value = (rshade, gshade, bshade)
                    pix[(startx + x) % im_size, (starty + y) % im_size] = value 

    plt.imshow(im)
    plt.show()

    time.sleep(.5)

目前,其绘制如下...

enter image description here

但是我希望每个框都覆盖前一个框,所以看起来好像一个框每隔0.5秒更改一次颜色

1 个答案:

答案 0 :(得分:0)

好。我找到了解决方案。它基于https://matplotlib.org/examples/animation/dynamic_image2.html

from PIL import Image
import random
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow, show
import matplotlib.animation as animation

%matplotlib notebook

def next_shape(im, num_squares, sq_size):
    pix = im.load()

    #initialise plot location
    startx, starty = 0, 0

    for i in range(num_squares):
        startx += sq_size
        for j in range(num_squares):
            starty += sq_size
            rshade = np.random.randint(0, 256)
            gshade = np.random.randint(0, 256)
            bshade = np.random.randint(0, 256)

            for x in range(sq_size):
                for y in range(sq_size):
                    value = (rshade, gshade, bshade)
                    pix[(startx + x) % im_size, (starty + y) % im_size] = value 

    #return list of pixel tuples
    return list(im.getdata())

# First set up the figure, the axis, and the plot element we want to animate
sq_size = 20
num_squares = 5

#create a figure to animate
fig = plt.figure()

#create the image withi the figure to animate
im_size = sq_size * num_squares
im = Image.new('RGB', (im_size, im_size))

#create a list to store all the images in the format of a list of RGB pixels
im_list_pix = []

#generate a bunch of images in the form of a list of RGB pixels
for pic in range(10):
    im_list_pix.append(next_shape(im, 5, 20))

#create a list to store images converted from RGB pixel tuples to image format
img_array = []

#convert list of pixel tuples back to image
for i, v in enumerate(im_list_pix):
    im = Image.new('RGB', (100, 100))
    #put the pixel data into the image container
    im.putdata(im_list_pix[i])

    im = plt.imshow(im)
    img_array.append([im])  

ani = animation.ArtistAnimation(fig, img_array, interval=500)

plt.show()

我花了很长时间才在...中找出方括号。

img_array.append([im])

...至关重要。必须是matplotlib.animate解析列表的方式。

我希望这对希望使用matplot.animate进行支付并拥有一些创造艺术乐趣的人有用。

PS-谢谢那些向我指出matplotlib.animate的评论者,也感谢这篇很棒的博客文章的作者,它为我提供了一些线索,并显示了您可以将这些东西推到多远http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/