1

我想绘制特定几何(多边形)的等高线图。我有角的坐标和这个多边形内的一些点,我想插入到等高线图中的一维参数。我能够绘制参数的分布,但图像显示为正方形(因为我不知道如何指定我的几何图形)。不用说我是 Python 初学者......

我目前使用以下代码;

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

r = np.array([[0, 0, 1.0000], [0, 1.0000, 0], [1.0000, 0, 0], [0, 0.7071, 0.7071],
[0, -0.7071, 0.7071],[0.7071, 0, 0.7071], [-0.7071, 0, 0.7071], [0.7071, 0.7071, 0], 
[-0.7071, 0.7071, 0], [0.8361,  0.3879, 0.3879], [-0.8361, 0.3879, 0.3879], 
[0.8361, -0.3879, 0.3879], [-0.8361, -0.3879, 0.3879], [0.3879, 0.8361, 0.3879],
[-0.3879, 0.8361, 0.3879], [0.3879, -0.8361, 0.3879], [-0.3879, -0.8361, 0.3879],
[0.3879, 0.3879, 0.8361], [-0.3879, 0.3879, 0.8361], [0.3879, -0.3879, 0.8361],
[-0.3879, -0.3879, 0.8361], [-1.0000, 0, 0], [-0.7071, -0.7071, 0], [0, -1.0000, 0],
[0.7071, -0.7071, 0]])

xx = r[:,0]
yy = r[:,1]
zz = r[:,2]

xxi, yyi = np.linspace(xx.min(), xx.max(), 100), np.linspace(yy.min(), yy.max(), 100)
xxi, yyi = np.meshgrid(xxi, yyi)

rbff = scipy.interpolate.Rbf(xx, yy, zz, function='linear')
zzi = rbff(xxi, yyi)
plt.imshow(zzi, vmin=zz.min(), vmax=zz.max(), origin='lower',
       extent=[xx.min(), xx.max(), yy.min(), yy.max()])
plt.scatter(xx, yy, c=zz)
plt.colorbar()
plt.show()   
4

2 回答 2

1

这是作弊,但是:您可以添加类似的内容

zzi = rbff(xxi, yyi)
zzi[zzi<0.1]=nan

并使用该值(目前为 0.1)。

于 2013-11-11T20:17:47.080 回答
0

如果您的多边形是凸面的,您可以使用它scipy.interpolate.griddata来获取遮罩区域:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

r = np.array([[0, 0, 1.0000], [0, 1.0000, 0], [1.0000, 0, 0], [0, 0.7071, 0.7071],
[0, -0.7071, 0.7071],[0.7071, 0, 0.7071], [-0.7071, 0, 0.7071], [0.7071, 0.7071, 0], 
[-0.7071, 0.7071, 0], [0.8361,  0.3879, 0.3879], [-0.8361, 0.3879, 0.3879], 
[0.8361, -0.3879, 0.3879], [-0.8361, -0.3879, 0.3879], [0.3879, 0.8361, 0.3879],
[-0.3879, 0.8361, 0.3879], [0.3879, -0.8361, 0.3879], [-0.3879, -0.8361, 0.3879],
[0.3879, 0.3879, 0.8361], [-0.3879, 0.3879, 0.8361], [0.3879, -0.3879, 0.8361],
[-0.3879, -0.3879, 0.8361], [-1.0000, 0, 0], [-0.7071, -0.7071, 0], [0, -1.0000, 0],
[0.7071, -0.7071, 0]])

xx = r[:,0]
yy = r[:,1]
zz = r[:,2]

xxi, yyi = np.linspace(xx.min(), xx.max(), 100), np.linspace(yy.min(), yy.max(), 100)
xxi, yyi = np.meshgrid(xxi, yyi)

rbff = scipy.interpolate.Rbf(xx, yy, zz, function='linear')
zzi = rbff(xxi, yyi)
mask = np.isnan(scipy.interpolate.griddata(np.c_[xx, yy], zz, (xxi, yyi)))
zzi[mask] = np.nan
plt.imshow(zzi, vmin=zz.min(), vmax=zz.max(), origin='lower',
       extent=[xx.min(), xx.max(), yy.min(), yy.max()])
plt.scatter(xx, yy, c=zz)
plt.colorbar()
plt.show() 

输出:

在此处输入图像描述

于 2013-11-12T03:52:45.360 回答