画等高线用scipy库插值报错?

scipy插值出现错误?

出错位置:

zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')

运行官网irregulardatagrid.py文件同样报错:

AttributeError: 'NoneType' object has no attribute 'close'
Exception ignored in: 'scipy.spatial.qhull._Qhull.__dealloc__'
Traceback (most recent call last):
  File "C:\Users\中文\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scipy\interpolate\ndgriddata.py", line 220, in griddata
    ip = LinearNDInterpolator(points, values, fill_value=fill_value,
AttributeError: 'NoneType' object has no attribute 'close'
Traceback (most recent call last):
  File "C:\Users\中文\Desktop\pictures\contour.py", line 19, in <module>
    X,Y,Z = plot_contour(x,y,z,resolution = 50,contour_method='linear')
  File "C:\Users\中文\Desktop\pictures\contour.py", line 16, in plot_contour
    Z = griddata(points, z, (X, Y), method=contour_method)
  File "C:\Users\中文\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scipy\interpolate\ndgriddata.py", line 220, in griddata
    ip = LinearNDInterpolator(points, values, fill_value=fill_value,
  File "interpnd.pyx", line 248, in scipy.interpolate.interpnd.LinearNDInterpolator.__init__
  File "qhull.pyx", line 1839, in scipy.spatial.qhull.Delaunay.__init__
  File "qhull.pyx", line 271, in scipy.spatial.qhull._Qhull.__init__
  File "messagestream.pyx", line 36, in scipy._lib.messagestream.MessageStream.__init__
OSError: Failed to open file b'C:\\Users\\\xe6\xb1\xa4\xe4\xb8\x9a\xe9\xb9\x8f\\AppData\\Local\\Temp\\scipy-2gut0q3u'
请按任意键继续. . .

请问出现这个错误是什么原因呢?我用matplotlib官网irregulardatagrid.py文件同样报错。
是否应为b’C:\Users\\xe6\xb1\xa4\xe4\xb8\x9a\xe9\xb9\x8f\AppData\Local\Temp\scipy-2gut0q3u’这里有中文路径?如果是应该在哪里改呢?

matplotlib官网irregulardatagrid.py文件:

"""
=======================================
Contour plot of irregularly spaced data
=======================================

Comparison of a contour plot of irregularly spaced data interpolated
on a regular grid versus a tricontour plot for an unstructured triangular grid.

Since `~.axes.Axes.contour` and `~.axes.Axes.contourf` expect the data to live
on a regular grid, plotting a contour plot of irregularly spaced data requires
different methods. The two options are:

* Interpolate the data to a regular grid first. This can be done with on-board
  means, e.g. via `~.tri.LinearTriInterpolator` or using external functionality
  e.g. via `scipy.interpolate.griddata`. Then plot the interpolated data with
  the usual `~.axes.Axes.contour`.
* Directly use `~.axes.Axes.tricontour` or `~.axes.Axes.tricontourf` which will
  perform a triangulation internally.

This example shows both methods in action.
"""

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np

np.random.seed(19680801)
npts = 200
ngridx = 100
ngridy = 200
x = np.random.uniform(-2, 2, npts)
y = np.random.uniform(-2, 2, npts)
z = x * np.exp(-x**2 - y**2)

fig, (ax1, ax2) = plt.subplots(nrows=2)

# -----------------------
# Interpolation on a grid
# -----------------------
# A contour plot of irregularly spaced data coordinates
# via interpolation on a grid.

# Create grid values first.
xi = np.linspace(-2.1, 2.1, ngridx)
yi = np.linspace(-2.1, 2.1, ngridy)

# Linearly interpolate the data (x, y) on a grid defined by (xi, yi).
#triang = tri.Triangulation(x, y)
#interpolator = tri.LinearTriInterpolator(triang, z)
#Xi, Yi = np.meshgrid(xi, yi)
#zi = interpolator(Xi, Yi)

# Note that scipy.interpolate provides means to interpolate data on a grid
# as well. The following would be an alternative to the four lines above:
#######'linear'  'nearest' 'cubic' 
from scipy.interpolate import griddata
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')

ax1.contour(xi, yi, zi, levels=14, linewidths=0.5, colors='k')
cntr1 = ax1.contourf(xi, yi, zi, levels=14, cmap="RdBu_r")

fig.colorbar(cntr1, ax=ax1)
ax1.plot(x, y, 'ko', ms=3)
ax1.set(xlim=(-2, 2), ylim=(-2, 2))
ax1.set_title('grid and contour (%d points, %d grid points)' %
              (npts, ngridx * ngridy))

# ----------
# Tricontour
# ----------
# Directly supply the unordered, irregularly spaced coordinates
# to tricontour.

ax2.tricontour(x, y, z, levels=14, linewidths=0.5, colors='k')
cntr2 = ax2.tricontourf(x, y, z, levels=14, cmap="RdBu_r")

fig.colorbar(cntr2, ax=ax2)
ax2.plot(x, y, 'ko', ms=3)
ax2.set(xlim=(-2, 2), ylim=(-2, 2))
ax2.set_title('tricontour (%d points)' % npts)

plt.subplots_adjust(hspace=0.5)
plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions and methods is shown in this example:

import matplotlib
matplotlib.axes.Axes.contour
matplotlib.pyplot.contour
matplotlib.axes.Axes.contourf
matplotlib.pyplot.contourf
matplotlib.axes.Axes.tricontour
matplotlib.pyplot.tricontour
matplotlib.axes.Axes.tricontourf
matplotlib.pyplot.tricontourf
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值