原来,您需要执行ax.add_artist(Rectangle)
以使颜色规范起作用;当使用patches.append(Rectangle)
时,矩形显示为蓝色(至少在我的PC上)忽略任何颜色规格。
顺便说一句,请注意artists — Matplotlib 1.2.1 documentation: class matplotlib.patches.Rectangle指出,有
edgecolor
- 中风颜色
facecolor
- 用于填充颜色
...再有就是color
- 这基本上套中风和填充颜色在同一时间。
下面是修改的OP代码,我已经在Linux(Ubuntu的11.04)测试,蟒2.7,matplotlib 0.99.3:
import matplotlib.pyplot as plt
import matplotlib.collections as collections
import matplotlib.ticker as ticker
import matplotlib
print matplotlib.__version__ # 0.99.3
fig=plt.figure() #pylab.figure()
ax=fig.add_subplot(111)
ax.set_xlim([-400, -380]) #pylab.xlim([-400, 400])
ax.set_ylim([-400, -380]) #pylab.ylim([-400, 400])
patches = []
polygon = plt.Rectangle((-400, -400), 10, 10, color='yellow') #Rectangle((-400, -400), 10, 10, color='y')
patches.append(polygon)
pol2 = plt.Rectangle((-390, -390), 10, 10, facecolor='yellow', edgecolor='violet', linewidth=2.0)
ax.add_artist(pol2)
p = collections.PatchCollection(patches) #, cmap=matplotlib.cm.jet)
ax.add_collection(p)
ax.xaxis.set_major_locator(ticker.MultipleLocator(20)) # (MultipleLocator(20))
ax.yaxis.set_major_locator(ticker.MultipleLocator(20)) # (MultipleLocator(20))
plt.show() #pylab.show()
这是输出: