开始之前,交代一下开发环境。个人的体会是,由于版本兼容问题,学Python经常遇到莫名其妙的现象。
环境:
win7 专业版 64位 Python 2.7.14 Ipython 5.5.0 pip 9.0.1 matplotlib 2.1.0 numpy 1.13.3 PIL 1.1.7 Pillow 4.3.0
以下代码亲测可用
from numpy import *
from pylab import *
from PIL import Image
# 加载本地图片,当心中文字符
im = array(Image.open(unicode("f:\\桌面\\test.jpg", 'utf-8')))
imshow(im)
x=[100,100,300,300]
y=[200,300,200,300]
# 在图片上绘制点和直线
plot(x,y,'r*')
plot(x[:2],y[:2])
# 标题和坐标轴
title(unicode("DARK KNIGHT", 'utf-8'))
axis('off')
show()

在让上述代码正常工作前,我遇到了一些问题,记录如下以备查阅:
Q: 如何查看已经安装的python软件包和版本
A: pip list
Q: 如何安装matplotlib
A: python -mpip install -U matplotlib
参见 http://matplotlib.org/users/installing.html
Q: name array is not defined
A: from numpy import *
Q: 使用imshow()函数时提示 PIL Image data can not convert to float
A: python -mpip install pillow