Unintentionally plotting multiple lines in matplotlib

Viewed 50

I am still getting familiar with the matplotlib library and I'm encountering a problem I've never had before. My intention is to graph the relative error associated with the scipy.integrate.quadrature method as a function of its tolerance value. What I'm encountering when I plot it, however, are two distinct lines from the same plt.plot command. How did this happen? I'll put my code in below.

from scipy import integrate

def f(x):<br/>
    return np.sqrt(1 - x**2)<br/>

xlist = []<br/>
for i in range (-12, 0):<br/>
    xlist.append(i)<br/>

tolerancelist = []<br/>
for i in xlist:<br/>
    tolerancelist.append(10**i)<br/>

ylist = []<br/>
for i in tolerancelist:<br/>
    q = integrate.quadrature(f, -1, 1, tol=i)<br/>
    ylist.append(q)<br/>
       
plt.plot(tolerancelist, ylist, label='line1')<br/>
legend = plt.legend(loc='best')
1 Answers

If you print ylist, you'll get a better understanding of what is going on.

[(1.5708027245307299, 3.9572988685954158e-07), 
(1.5708027245307299, 3.9572988685954158e-07), 
(1.5708027245307299, 3.9572988685954158e-07), 
(1.5708027245307299, 3.9572988685954158e-07), 
(1.5708027245307299, 3.9572988685954158e-07), 
(1.5708027245307299, 3.9572988685954158e-07),
(1.5708087325834776, 9.6666040283466259e-07), 
(1.5708599005218433, 8.8700570273214652e-06), 
(1.571132830068839, 8.7760917619084111e-05), 
(1.5721552241258274, 0.00062673127406376317),
(1.5759063348593505, 0.0043711928407235145), 
(1.5916172578151968, 0.041375904040254818)]

Each value in ylist is actually tuple of two values. Matplotlib is plotting both as two separate lines.

Related