创建视图和副本

来源:互联网 发布:mac原唱变伴奏 编辑:程序博客网 时间:2024/05/03 21:25

区分清楚我们是与共享的视图打交道还是获得数据的一个副本。一个切片代表一个视图,如果把一个切片赋值给另一个变量,随后改变切片在数组中的内容,那么这个变量值也会改变。

“`python
author = ‘guoguo’

encoding:utf-8

import scipy.misc
import matplotlib.pyplot

lena=scipy.misc.lena()
acopy=lena.copy()#创建一个副本
aview=lena.view()#创建一个视图

drawing

matplotlib.pyplot.subplot(221)
matplotlib.pyplot.imshow(lena)

matplotlib.pyplot.subplot(222)
matplotlib.pyplot.imshow(acopy)

matplotlib.pyplot.subplot(223)
matplotlib.pyplot.imshow(aview)

aview.flat=0#flat迭代器,视图中所有值都会清零
matplotlib.pyplot.subplot(224)
matplotlib.pyplot.imshow(aview)

matplotlib.pyplot.show()
“`运行结果图

0 0