✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Streamline - truth value of an array

Hello,
I’m new to plotly,
i’m having trouble with some code, receiving this error message :

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

About this code :

def chp_magnetique2():
os.chdir(‘fichier’)
Ur = xlrd.open_workbook(‘données sur Ur.xlsx’)
Uz = xlrd.open_workbook(‘données sur Uz.xlsx’)
ur = Ur.sheet_by_name(u’Sheet1’)
uz = Uz.sheet_by_name(u’Sheet1’)
lr=ur.nrows
lz=uz.nrows
mr2=[]
mz2=[]
for k in range (1,lr):
mr2=mr2+[ur.row_values(k)[1:ur.ncols]]
for i in range(1,lz):
mz2=mz2+[uz.row_values(i)[1:uz.ncols]]
plotly.tools.set_credentials_file(username=‘name’, api_key=‘key’)
x = np.linspace(0, 110.3, 5.8)
y = np.linspace(0, 110.3, 5.8)
Y, X = np.meshgrid(x, y)
u = mr2
v = mz2
fig = ff.create_streamline(X, Y, u,v,arrow_scale=.1)
py.plot(fig,filename=‘6emeessai’)

do you know how to fix it ?
Thank you

Yes, there is a way to fix this!

First of all, locate where in the code you are getting the error (the line number). This error occurs when you are assigning a boolean True or False to an array such as [1, 5, 3, 2, 1]. This logically can’t be done, so instead you must look at the individual elements inside the array. For instance:

if all(item == 5 for item in array)
    print 'all good'

Hope this makes sense to you.

Thank you for the answer, i understood your point but i don’t understand why a boolean is assigned to my array
(the error occurs line : fig = ff.create_streamline(X, Y, u,v,arrow_scale=.1) )

Ok, i fixed it
Thank you !

Did you fix it in your code or in the create_streamline code? I am having this issue as well and it definitely happens within create_streamline, see below. This change seems to fix it:

- if ((x[index + 1] - x[index]) - (x[1] - x[0])) > 0.0001:
+ if any(((x[index + 1] - x[index]) - (x[1] - x[0])) > 0.0001):
import plotly.figure_factory as ff
import numpy as np

x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y

fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)

~/.virtualenvs/onbeach/local/lib/python3.7/site-packages/plotly/figure_factory/_streamline.py in create_streamline(x, y, u, v, density, angle, arrow_scale, **kwargs)
    112     utils.validate_equal_length(x, y)
    113     utils.validate_equal_length(u, v)
--> 114     validate_streamline(x, y)
    115     utils.validate_positive_scalars(density=density, arrow_scale=arrow_scale)
    116 

~/.virtualenvs/onbeach/local/lib/python3.7/site-packages/plotly/figure_factory/_streamline.py in validate_streamline(x, y)
     26         raise ImportError("FigureFactory.create_streamline requires numpy")
     27     for index in range(len(x) - 1):
---> 28         if ((x[index + 1] - x[index]) - (x[1] - x[0])) > 0.0001:
     29             raise exceptions.PlotlyError(
     30                 "x must be a 1 dimensional, " "evenly spaced array"

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()