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

标签 python numpy matplotlib scipy contour

您好,我正在尝试绘制不均匀数据的填充轮廓。这是三个列表中的。我的问题是我无法获得平滑的填充轮廓。我所做的是首先使用 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的插值方法可以设置为“三次”。这会产生更好的结果,并且您可以将采样大小保留为以前的大小。

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/

相关文章:

python - 检查一个数据框中的列对是否存在于另一个数据框中?

python - 在样条拟合的一维数据中找到拐点

python - 使用 matplotlib 堆积条形图

python - 在 Squarify 中设置字体和换行

python - matplotlib 中文本的部分着色

python - 枢轴类属性

javascript - 将 highchart 中的 x y 点绘制为 [[x 点列表],[y 点列表]]

python - 找不到一个或多个组件。安装 python 工具时出现错误,请重新安装应用程序

python - 使用 SELECT 语句的 MySQL ProgrammingError 1064

python - 如何使用Numpy解决Type Error