我正在尝试返回轮廓的z-value
。我想用来返回z-value
的特定点是从['C1_X'],['C1_Y']
中的df
调用的。当这些坐标按升序排列时,代码可以工作,但是当降序排列时,它将引发错误。
错误:
raise ValueError("Error code returned by bispev: %s" % ier)
ValueError: Error code returned by bispev: 10
用于返回z值的代码为:
# Return z-value for C coordinate
f = RectBivariateSpline(X[0, :], Y[:, 0], normPDF.T)
z = f(d['C1_X'], d['C1_Y'])
print(z)
使用@Prasanth的建议。是否可以逐帧通过f
?因此,传递第一帧,清除或清空输入,然后传递第二帧,等等。
这样的作品行吗?
f = RectBivariateSpline(X[0, :], Y[:, 0], normPDF.T)
z = f(d1['C1_X'], d1['C1_Y'])
z = np.empty(f(d1['C1_X'], d1['C1_Y']))
z = zip(d['C1_X'], d['C1_Y'])
print(z)
下面是整个代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as sts
import matplotlib.animation as animation
from scipy.interpolate import RectBivariateSpline
DATA_LIMITS = [0, 20]
def datalimits(*data):
return DATA_LIMITS
def mvpdf(x, y, xlim, ylim, radius=1, velocity=0, scale=0, theta=0):
X,Y = np.meshgrid(np.linspace(*xlim), np.linspace(*ylim))
XY = np.stack([X, Y], 2)
x,y = (x, y)
PDF = sts.multivariate_normal([x, y]).pdf(XY)
return X, Y, PDF
def mvpdfs(xs, ys, xlim, ylim, radius=None, velocity=None, scale=None, theta=None):
PDFs = []
for i,(x,y) in enumerate(zip(xs,ys)):
kwargs = {
'xlim': xlim,
'ylim': ylim
}
X, Y, PDF = mvpdf(x, y,**kwargs)
PDFs.append(PDF)
return X, Y, np.sum(PDFs, axis=0)
fig, ax = plt.subplots(figsize = (10,4))
ax.set_xlim(DATA_LIMITS)
ax.set_ylim(DATA_LIMITS)
line_a, = ax.plot([], [], '.', c='red', alpha = 0.5, markersize=5, animated=True)
line_b, = ax.plot([], [], '.', c='blue', alpha = 0.5, markersize=5, animated=True)
lines=[line_a,line_b]
scat = ax.scatter([], [], s=20, marker='o', c='white', alpha = 1,zorder=3)
scats=[scat]
cfs = None
def plotmvs(tdf, xlim=None, ylim=None, fig=fig, ax=ax):
global cfs
if cfs:
for tp in cfs.collections:
tp.remove()
df = tdf[1]
if xlim is None: xlim = datalimits(df['X'])
if ylim is None: ylim = datalimits(df['Y'])
PDFs = []
for (group, gdf), group_line in zip(df.groupby('group'), lines+scats):
if group in ['A','B']:
group_line.set_data(*gdf[['X','Y']].values.T)
kwargs = {
'xlim': xlim,
'ylim': ylim
}
X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, **kwargs)
PDFs.append(PDF)
elif group in ['C']:
gdf['X'].values, gdf['Y'].values
scat.set_offsets(gdf[['X','Y']].values)
normPDF = (PDFs[0]-PDFs[1])/max(PDFs[0].max(),PDFs[1].max())
f = RectBivariateSpline(X[0, :], Y[:, 0], normPDF.T)
z = f(d['C1_X'], d['C1_Y'])
print(z)
cfs = ax.contourf(X, Y, normPDF, cmap = 'jet', alpha = 1, levels=np.linspace(-1,1,10),zorder=1)
return cfs.collections + [scat] + [line_a,line_b]
n = 9
time = range(n)
d = ({
'A1_X' : [13,14,12,13,11,12,13,12,11,10],
'A1_Y' : [6,6,7,7,7,8,8,8,9,10],
'A2_X' : [7,6,5,7,6,3,4,5,6,6],
'A2_Y' : [11,12,11,10,11,12,10,11,10,9],
'B1_X' : [8,9,8,7,6,7,5,6,7,6],
'B1_Y' : [3,4,3,2,3,4,2,1,2,3],
'B2_X' : [13,14,14,14,13,13,13,12,12,12],
'B2_Y' : [5,4,3,2,4,5,4,6,3,3],
'C1_X' : [8,9,9,10,11,12,12,13,14,14],
'C1_Y' : [3,3,5,5,6,8,9,10,11,11],
})
tuples = [((t, k.split('_')[0][0], int(k.split('_')[0][1:]), k.split('_')[1]), v[i])
for k,v in d.items() for i,t in enumerate(time) ]
df = pd.Series(dict(tuples)).unstack(-1)
df.index.names = ['time', 'group', 'id']
interval_ms = 1000
delay_ms = 2000
ani = animation.FuncAnimation(fig, plotmvs, frames=df.groupby('time'), interval=interval_ms, repeat_delay=delay_ms,)
plt.show()
答案 0 :(得分:2)
错误代码10表示输入无效(Google搜索!)。
创建玩具数据。您的代码正在做很多事情,我想隔离导致错误的部分。
X,Y = np.meshgrid(np.linspace(0, 20), np.linspace(0, 20))
创建一个可调用的值以在输入值处进行插值。
from scipy.interpolate import RectBivariateSpline
f = RectBivariateSpline(X[0, :], Y[:, 0], z = X**2+Y**2)
尝试运行代码。
f([10,10,10,10,10,10,10,10,10,10], [10,10,10,10,10,10,10,10,10,9])
截断的输出
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-84-73aaa29ad73f> in <module>
----> 1 f([10,10,10,10,10,10,10,10,10,10], [10,10,10,10,10,10,10,10,10,9])
检查f
的文档字符串以查看是否有某些信息。我正在Jupyter笔记本中进行此操作。
f?
截断的输出
Signature: f(x, y, dx=0, dy=0, grid=True)
Type: RectBivariateSpline
String form: <scipy.interpolate.fitpack2.RectBivariateSpline object at 0x7f5a4a7f9cc0>
File: ~/bin/anaconda3/envs/py37a/lib/python3.7/site-packages/scipy/interpolate/fitpack2.py
Docstring:
Bivariate spline approximation over a rectangular mesh.
Can be used for both smoothing and interpolating data.
Parameters
----------
x,y : array_like
1-D arrays of coordinates in strictly ascending order.
...
...
...
哦。它希望也以严格升序的方式对这些点进行评估。那么订购要评估的积分会起作用吗?
f([10,10,10,10,10,10,10,10,10,10], [9, 10,10,10,10,10,10,10,10,10])
输出
array([[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.],
[181., 200., 200., 200., 200., 200., 200., 200., 200., 200.]])
在代码工作中调用插值函数工作之前,将对要执行插值的点进行排序吗?
希望这会有所帮助。
编辑
这是我的意思:
f = RectBivariateSpline(X[0, :], Y[:, 0], normPDF.T)
# z = f(d['C1_X'], d['C1_Y'])
x_ip = d['C1_X']
y_ip = d['C1_Y']
z = np.empty((len(x_ip), len(y_ip)), dtype=np.float64)
for i, x in enumerate(x_ip):
z[i, :] = f([x], y_ip) # ASSUME THAT y_ip IS SORTED.
print(z)
通过此更改,我的计算机上运行了您的代码:它显示了一些轮廓动画。将此思想适应于x和y不能都按升序排列的其他情况。您可以循环遍历x和y,但这会增加函数调用的次数。