把数据数组传给交互函数?

2024-06-08 21:50:20 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以将数据数组(numpy数组)传递给interact函数? 我的数据从csv文件bad.csv加载并存储到numpy数组中:uvw和{}。在

我尝试过使用以下代码片段,但运气不佳:

%matplotlib inline

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

from IPython.html.widgets import interact, fixed

fileName = './bad.csv'
p,u,v,w,x,y,z  = np.loadtxt(fileName,delimiter=',',skiprows=1,usecols=(0,1,2,3,4,5,6),unpack=True)

uNorm = np.sqrt(u**2 + v**2 + w**2)
r = np.sqrt(y**2 + x**2)
tang = (y / x)

aux_tri = mtri.Triangulation(r/np.max(r), tang/np.max(tang))
triang = mtri.Triangulation(x, y, aux_tri.triangles)
triang.set_mask(mtri.TriAnalyzer(aux_tri).get_flat_tri_mask())

def plotContour(data=w, legendName='Legend', gridOn=True, edgeColors='black'):
    plt.gca().set_aspect('equal')
    plt.tripcolor(triang,data,NbLevels,cmap=cm.hot_r,edgecolors=edgeColors)
    plt.grid(gridOn)
    cbar = plt.colorbar()
    cbar.set_label(legendName,labelpad=10)

interact(plotContour, data={'u':fixed(u),'v':fixed(v),'w':fixed(w),'Magnitude':fixed(uNorm)}, edgeColors=('Black','None'));

第一次运行此代码段时,由于data的默认值是w,所以一切正常。在

enter image description here

但是当我尝试使用下拉菜单时,我遇到了以下错误:

^{pr2}$

那么如何在下拉菜单中传递numpy数组呢?在


Tags: csvimportnumpydatamatplotlibasnpplt