在屏幕分辨率下绘制填充轮廓

时间:2014-09-20 13:23:03

标签: python matplotlib plot contour contourf

我正在尝试使用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()

以上是我的代码的基本思想。但我没有得到怎么做。请帮忙

1 个答案:

答案 0 :(得分: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)

enter image description here

此时,您可以使用imshowpcolorzi显示为图片:

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

enter image description here

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