如何避免pcolor()/ imshow()和contour()叠加之间的偏移?

时间:2013-05-24 22:06:48

标签: python matplotlib plot

我想展示一个覆盖有轮廓线的伪彩色图像(例如由pcolor,pcolormesh或imshow生成)。看来这三个绘图函数可以是一个数据点关闭。这是一个例子:

import numpy
from matplotlib import pyplot,cm

f = pyplot.figure(figsize=(3,2))
ax = f.add_subplot(111)

data = numpy.ones((10,10))
data[5,5] = 2.0
data[0,:] = data[-1,:] = 0
data[:,0] = data[:,-1] = 0

X=numpy.linspace(-3,3,10)
Y=numpy.linspace(-2,2,10)

ax.imshow(data,interpolation='nearest',extent=(-3,3,-2,2),aspect='auto')
ax.contour(X,Y,data,cmap=cm.Greys)
f.subplots_adjust(bottom=.2)

pyplot.show()

这产生(使用TkAgg后端GUI):

imshow() example

代替imshow()方法
ax.pcolormesh(X,Y,data)

的产率:

pcolormesh() example

在这两种情况下,轮廓线都与底层图像不匹配。

1 个答案:

答案 0 :(得分:0)

import numpy
from matplotlib import pyplot,cm

f = pyplot.figure()
ax = f.add_subplot(111)

data = numpy.ones((10,10))
data[5,5] = 2.0
data[0,:] = data[-1,:] = 0
data[:,0] = data[:,-1] = 0

ax.imshow(data, interpolation='nearest')
ax.contour(data,levels=[0.0,0.5,1.0,1.5,2.5],cmap=cm.Greys)
ax.invert_yaxis()
pyplot.show()

enter image description here