Python 在matplotlib中使用不规则采样覆盖图像和打印
我有来自模拟的数据,其中结果是两个参数的函数,Python 在matplotlib中使用不规则采样覆盖图像和打印,python,image,matplotlib,plot,Python,Image,Matplotlib,Plot,我有来自模拟的数据,其中结果是两个参数的函数,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,
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
中是否有其他我可以使用的命令?您想要使用的命令,需要输入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()