我有来自模拟的数据,其中结果是 2 个参数的函数,xval
并且yval
. 的抽样是yval
有规律的,但抽样xval
是不规则的。
我有xval
和yval
对的每个组合的模拟结果,我可以用等高线图绘制结果。这是一个简化的示例:
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
方法不将xval
和yval
值作为参数,因此它不知道在哪个(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] 删除。
我来说两句