嗨,iam试图绘制不均匀数据的填充轮廓。在三个列表中。我的问题是我无法获得平滑的填充轮廓。我首先要做的是使用griddata将数据从不规则点更改为网格。

import numpy as np
import matplotlib.pyplot as plt
import time as time
from scipy.interpolate import griddata
x = [39, 39, 603, 603, 540.8578720591851, 586.349172503832, 373.99215228030187, 436.4554443169055, 125.7177128362948, 56.44720056160912, 453.35159098310174, 384.081128192362, 51.846094630755104, 121.11660875746472, 278.0734642496455, 211.33415130113278, 508.642428513517, 453.0506702655636, 455.66065332357397, 381.7443137710119, 211.08060937414135, 271.19278437560484, 301.7212739516758, 337.50499942076925, 237.27644459337762, 277.8143694411149, 89.76821876085899, 145.66110067318877, 151.97990283138796, 197.59696541916784, 398.0895764975718, 453.7365065456195]
y = [-29, 394, -29, 394, 96.31199431392861, 96.31199431392861, 65.63484056949213, 65.63484056949213, 353.9802948050525, 353.99631296027843, 354.83809861715105, 354.75376513965614, 170.85745938538898, 170.85745938538898, 156.95287962269862, 156.95287962269862, 161.4804871844196, 160.98633822221555, 242.17985596076556, 241.74154302501933, 214.02665403095247, 214.02665403095247, 65.63484056949213, 65.63484056949213, 63.49457402918261, 63.49457402918261, 54.22008568784131, 54.22008568784131, 7.134221801031751, 7.134221801031751, 3.5671109005158756, 3.5671109005158756]
z = [0, 0, 0, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
extent = (min(x), max(x), min(y), max(y))
xs,ys = np.mgrid[extent[0]:extent[1], extent[2]:extent[3]]
resampled = griddata((x, y), z, (xs, ys))
plt.figure()
plt.imshow(resampled.T, extent=(min(x), max(x), max(y), min(y)))
plt.hold(True)
plt.scatter(x,y,c=z)
plt.show()


如下图所示。

看起来很奇怪。如何将颜色变化平滑到下一个点。
提前致谢

最佳答案

您的采样网格太密集,因此imshow根本不需要插值。尺寸约为30x30点,看起来更好。另外,将imshow的插值方法设置为“ bicubic”。

编辑:我忽略了可以将griddata的插值方法设置为“三次”。这样可获得更好的结果,您可以像以前一样保留采样大小。

Edit2:看起来用我原来的方法可以达到最好的结果。让griddata通过线性插值在网格上生成样本,然后使用imshow或等高线基于该数据进行三次插值,即:

import numpy as np
import matplotlib.pyplot as plt
import time as time
from scipy.interpolate import griddata
x = [39, 39, 603, 603, 540.8578720591851, 586.349172503832, 373.99215228030187,436.4554443169055, 125.7177128362948, 56.44720056160912, 453.35159098310174, 384.081128192362, 51.846094630755104, 121.11660875746472, 278.0734642496455, 211.33415130113278, 508.642428513517, 453.0506702655636, 455.66065332357397, 381.7443137710119, 211.08060937414135, 271.19278437560484, 301.7212739516758, 337.50499942076925, 237.27644459337762, 277.8143694411149, 89.76821876085899, 145.66110067318877, 151.97990283138796, 197.59696541916784, 398.0895764975718, 453.7365065456195]
y = [-29, 394, -29, 394, 96.31199431392861, 96.31199431392861, 65.63484056949213, 65.63484056949213, 353.9802948050525, 353.99631296027843, 354.83809861715105, 354.75376513965614, 170.85745938538898, 170.85745938538898, 156.95287962269862, 156.95287962269862, 161.4804871844196, 160.98633822221555, 242.17985596076556, 241.74154302501933, 214.02665403095247, 214.02665403095247, 65.63484056949213, 65.63484056949213, 63.49457402918261, 63.49457402918261, 54.22008568784131, 54.22008568784131, 7.134221801031751, 7.134221801031751, 3.5671109005158756, 3.5671109005158756]
z = [0, 0, 0, 0, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
extent = (min(x), max(x), min(y), max(y))
xs,ys = np.mgrid[extent[0]:extent[1]:30j, extent[2]:extent[3]:30j]
resampled = griddata((x, y), z, (xs, ys))
plt.figure(figsize=(8,8))
plt.imshow(resampled.T, extent=(min(x), max(x), max(y), min(y)),interpolation='bicubic')
plt.contour(resampled.T, extent=(min(x), max(x), max(y), min(y)),interpolation='bicubic',origin='upper')
plt.hold(True)
plt.scatter(x,y,c=z)
plt.show()

关于python - 如何在matplotlib中获得平滑的填充轮廓?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20400641/

10-12 02:16