具有非线性x标度的等高线图

时间:2016-08-19 21:26:06

标签: python matplotlib

我正在研究两个图:顶部的等高线图和底部的x-y图contour plot

等高线图通过以下行

完成
plt.imshow(df, extent = [xmin, xmax, ymin, ymax])

而x,y图是

 xyplot = df.mean()
 plt.plot(x, xyplot)

并且应垂直对齐顶部的等高线图,但x-y图具有非线性x比例。下图显示了x轴作为其索引enter image description here

的函数

由于我无法为“imshow”方法的“extent”变量提供数组,因此无法在等高线图中提供某些x比例。如何在轮廓图上绘制一些非线性比例,以便两个图形在垂直轴上对齐?

1 个答案:

答案 0 :(得分:1)

您可以使用scipy.interpolate.interp2d在常规网格上插入图像。这是一个例子:

import numpy as np
import pylab as pl

x = np.linspace(0, 1, 100)
x2 = x ** 2
y = np.linspace(0, 1, 200)

X, Y = np.meshgrid(x, y)
X2, Y2 = np.meshgrid(x2, y)
Z = np.sin(10 * (X**2 + Y**2))
Z2 = np.sin(10 * (X2**2 + Y2**2))

from scipy import interpolate

i2d = interpolate.interp2d(x2, y, Z2)
Zi = i2d(x, y)

fig, axes = pl.subplots(1, 3, figsize=(12, 4))
extent = [0, 1, 0, 1]
axes[0].imshow(Z, extent=extent)
axes[1].imshow(Z2, extent=extent)
axes[2].imshow(Zi, extent=extent)

输出:

enter image description here

left:在常规网格上计算的数组。 center:无规则网格计算的数组。 right:在常规网格上插入中心数组的结果。