0

我正在尝试使用matplotlib. 我imshow用来绘制轮廓。如何以屏幕分辨率的大小绘制轮廓。

例如,(x,y)是坐标,z是位置的对应值:

import pylab as plt
import numpy as np
from Tkinter import *
root = Tk()
root.title("test window")
root.minsize(800, 600)
#(x,y) are pixel coordinates of canvas created on root, z= values
x=(563.603522282, 405.223798299,91.082456699,418.716359588,86.4813516941,244.703807775,480.84654939,418.702483547,241.136696875,319.613136686,257.545407017,117.714659717,174.788434125,425.913041522)
y=(96.3119943139,65.6348405695,353.988303883,354.795931878,170.857459385,156.952879623,161.233412703,241.960699493,214.026654031,65.6348405695,63.4945740292,54.2200856878,7.13422180103,3.56711090052)
z=(-1, 0, 0.5, -0.2, 1, 6, 0, -5, 4, 1.5, 2,7,-3,1)
Z=np.array((x,y,z)) #not sure from here
im = plt.imshow(Z, cmap='jet') 
root.crate_image(im) # the image created by imshow paste on canvas
mainloop()

以上是我的代码的基本思想。但我不知道该怎么做。请帮忙

4

1 回答 1

1

imshow想要一个NxM在太空中独立存在的阵列。

你有三个向量,所以你真正要问的这个问题是:

如何将具有随机空间分布的数据插入到规则间隔的网格中?

答案如下:

正如现在所写,您的数据最容易显示在散点图中:

import matplotlib.pyplot as plt # don't use pylab
import numpy as np

x= np.array([
    563.603522282, 405.223798299, 91.082456699,
    418.716359588, 86.4813516941, 244.703807775,
    480.84654939,  418.702483547, 241.136696875,
    319.613136686, 257.545407017, 117.714659717,
    174.788434125, 425.913041522
])
y = np.array([
    96.3119943139, 65.6348405695, 353.988303883,
    354.795931878, 170.857459385, 156.952879623,
    161.233412703, 241.960699493, 214.026654031,
    65.6348405695, 63.4945740292, 54.2200856878,
    7.13422180103, 3.56711090052
])

z = np.array([-1, 0, 0.5, -0.2, 1, 6, 0, -5, 4, 1.5, 2,7,-3,1])

# define grid on which you'll interpolate.
N = 100
xi = np.linspace(np.floor(x.min()), np.ceil(x.max()), N)
yi = np.linspace(np.floor(y.min()), np.ceil(y.max()), N)

# grid (interpolate) the data.
zi = plt.mlab.griddata(x, y, z, xi, yi, interp='linear')

# contour the gridded data, plotting dots at the nonuniform data points.
fig, ax = plt.subplots()
c = ax.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
cf = ax.contourf(xi, yi, zi, 15, cmap=plt.cm.coolwarm,  # seriously, don't use jet or rainbow or anything like that
                  vmax=abs(zi).max(), vmin=-abs(zi).max())
fig.colorbar(cf)  # draw colorbar
# plot data points.
ax.scatter(x, y, marker='o', c='b', s=5, zorder=10)

在此处输入图像描述

此时,您可以使用imshowpcolor显示zi为图像:

fig, ax = plt.subplots()
pc = ax.pcolor(xi, yi, zi, cmap=plt.cm.coolwarm)
fig.colorbar(pc)
fig.savefig('pcolor.png')

在此处输入图像描述

我使用pcolor是因为设置 x 轴和 y 轴的范围更直观。

于 2014-09-20T14:30:27.930 回答