gpt4 book ai didi

python - 如何在 matplotlib 中获得平滑的填充轮廓?

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:32 26 4
gpt4 key购买 nike

您好,我正在尝试绘制不均匀数据的填充轮廓。在三个列表中。我的问题是我无法获得平滑的填充轮廓。我所做的是首先使用 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()

给出的情节如下。 enter image description here这看起来很奇怪。我怎样才能将颜色变化平滑到下一点。提前致谢

最佳答案

您的采样网格太密集,以至于 imshow 根本不需要插值。尺寸大约为 30x30 点,它看起来好多了。此外,将 imshow 的插值方法设置为 'bicubic'。

编辑:我忽略了griddata的插值方法可以设置为'cubic'。这会产生更好的结果,您可以像以前一样保留采样大小。

Edit2:看起来最好的结果是用我原来的方法实现的。让 griddata 通过线性插值在网格上生成样本,并根据该数据使用 imshow 或 contour 进行三次插值,即:

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()

2d image plot

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com