最後,我必須自己插值。就我使用的線性軸而言,即使對於非常稀疏的網格,這種方法也可以很好地工作。請注意,下面是我對雙線性插值的天真嘗試,如果你碰巧有更好的算法(請不要像scipy等其他依賴),請張貼它。我還沒有在對數座標軸上測試過這個...
# note that z-data are indexed with pair (y, x) rather than (x, y)
pos = event.inaxes.transAxes.inverted().transform([event.x, event.y])
# relative coordinates in <0, 1>
rx = pos[0] * (self.__data.shape[1] - 1)
ry = pos[1] * (self.__data.shape[0] - 1)
# calculate the corner coordinates
rxLow, rxHigh = math.floor(rx), math.ceil(rx)
ryLow, ryHigh = math.floor(ry), math.ceil(ry)
# weights based on the distance
wxLow, wyLow = 1.0 - rx + rxLow, 1.0 - ry + ryLow
wxHigh, wyHigh = 1.0 - rxHigh + rx, 1.0 - ryHigh + ry
# adjust in case rxLow == rxHigh or ryLow == ryHigh
wx = wxLow + wxHigh
wy = wyLow + wyHigh
wxLow /= wx
wxHigh /= wx
wyLow /= wy
wyHigh /= wy
# get the corner data
z00 = self.__data[ryLow, rxLow]
z01 = self.__data[ryHigh, rxLow]
z10 = self.__data[ryLow, rxHigh]
z11 = self.__data[ryHigh, rxHigh]
# interpolate
zxLow = z00 * wyLow + z01 * wyHigh
zxHigh = z10 * wyLow + z11 * wyHigh
z = zxLow * wxLow + zxHigh * wxHigh # the interpolated value