在 matplotlib 中使用不规则采样叠加图像和绘图

亚瑟

我有来自模拟的数据,其中结果是 2 个参数的函数,xval并且yval. 的抽样是yval有规律的,但抽样xval是不规则的。

我有xvalyval对的每个组合的模拟结果,我可以用等高线图绘制结果。这是一个简化的示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors

xval = np.array([ 10,  15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 250, 500])
yval = np.array([ 6, 12, 16, 22, 26, 32, 38, 42, 48, 52, 58, 62, 68, 74, 78, 84, 88, 94])

xx, yy = np.meshgrid(xval, yval)
sim = np.exp(-(xx/20))   # very deterministic simulation!

levels = np.sort(sim.mean(axis=0))

plt.clf()
plt.contour(xval, yval, sim, colors='k', vmin=1e-10, vmax=sim.max(), levels=levels)

我故意将轮廓的级别设置为模拟的值。结果如下:

在此处输入图片说明

现在我想将sim数组作为图像叠加在这个等高线图上。我使用了以下命令:

plt.imshow(sim, interpolation='nearest', origin=1, aspect='auto', vmin=1e-10, vmax=sim.max(),
           extent=(xval.min(), xval.max(), yval.min(), yval.max()), norm=colors.LogNorm())

结果如下:

在此处输入图片说明

正如您所看到的,轮廓和sim数据在图中不匹配,尽管它们应该匹配。这看起来很正常,因为该imshow方法不将xvalyval值作为参数,因此它不知道在哪个(xval,yval)点提供模拟。

现在的问题是:我如何设法让我的图像数据与轮廓相匹配?我需要重新插入sim数组还是有其他一些matplotlib我可以使用的命令

存在的重要性欧内斯特

您想使用pcolormesh,它需要 x 和 y 坐标作为输入。

plt.pcolormesh(xx,yy,sim)

完整示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors

xval = np.array([ 10,  15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 250, 500])
yval = np.array([ 6, 12, 16, 22, 26, 32, 38, 42, 48, 52, 58, 62, 68, 74, 78, 84, 88, 94])

xx, yy = np.meshgrid(xval, yval)
sim = np.exp(-(xx/20.))   # very deterministic simulation!

levels = np.sort(sim.mean(axis=0))

plt.contour(xval, yval, sim, colors='k', vmin=1e-10, vmax=sim.max(), levels=levels)

plt.pcolormesh(xx,yy,sim,  vmin=1e-10, vmax=sim.max(), norm=colors.LogNorm())

plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章