Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: unsupported operand type(s) for -: 'list' and 'list' when using quiver matplotlib

I was running the code below and the following error message came up.

I saw this post which might be potentially a fix to the issue, but I wasn’t understanding how this can be applied to solve my issue. Any suggestion is appreciated.

TypeError: unsupported operand type(s) for -: 'list' and 'list'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import matplotlib.pyplot as plt

x1 = [ 0.5, 0.6, 0.3, 0.2 ]
x2 = [ 0.5, 0.6, 0.3, 0.2 ]
x3 = [ 0.2, 0.3, 0.5, 0.6 ]
x4 = [ 0.4, 0.3, 0.2, 0.1 ]

y1 = [ 0.7, 0.9, 0.1, 0.2 ]
y2 = [ 0.8, 0.5, 0.9, 0.2 ]
y3 = [ 0.6, 0.9, 0.1, 0.2 ]
y4 = [ 0.8, 0.2, 0.3, 0.5 ]

deltaX1, deltaX2, deltaX3, deltaX4 = [x[1:] - x[:-1] for x in [x1, x2, x3, x4]]
deltaY1, deltaY2, deltaY3, deltaY4 = [y[1:] - y[:-1] for y in [y1, y2, y3, y4]]

line1 = plt.plot(x1, y1,'bo-',label='apple') 
line2 = plt.plot(x2, y2,'go-',label='banana') 
line3 = plt.plot(x3, y3,'ko-',label='orange')
line4 = plt.plot(x4, y4,'ro-',label='tomato') 

arrows1 = plt.quiver(x1[:-1], y1[:-1], deltaX1, deltaY1, scale_units='xy', angles='xy', scale=1)
arrows2 = plt.quiver(x2[:-1], y2[:-1], deltaX2, deltaY2, scale_units='xy', angles='xy', scale=1)
arrows3 = plt.quiver(x3[:-1], y3[:-1], deltaX3, deltaY3, scale_units='xy', angles='xy', scale=1)
arrows4 = plt.quiver(x4[:-1], y4[:-1], deltaX4, deltaY4, scale_units='xy', angles='xy', scale=1)


plt.title("Fruits")
plt.ylabel("Tastiness")
plt.xlabel("Benefit")

plt.legend(bbox_to_anchor=(1.5, 1),
           bbox_transform=plt.gcf().transFigure)


Error Message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/gf/pvtbdsj96cz1tv7vmn1gq7j40000gn/T/ipykernel_27948/3259319199.py in <module>
     11 y4 = [ 0.8, 0.2, 0.3, 0.5 ]
     12 
---> 13 deltaX1, deltaX2, deltaX3, deltaX4 = [x[1:] - x[:-1] for x in [x1, x2, x3, x4]]
     14 deltaY1, deltaY2, deltaY3, deltaY4 = [y[1:] - y[:-1] for y in [y1, y2, y3, y4]]
     15 

/var/folders/gf/pvtbdsj96cz1tv7vmn1gq7j40000gn/T/ipykernel_27948/3259319199.py in <listcomp>(.0)
     11 y4 = [ 0.8, 0.2, 0.3, 0.5 ]
     12 
---> 13 deltaX1, deltaX2, deltaX3, deltaX4 = [x[1:] - x[:-1] for x in [x1, x2, x3, x4]]
     14 deltaY1, deltaY2, deltaY3, deltaY4 = [y[1:] - y[:-1] for y in [y1, y2, y3, y4]]
     15 

TypeError: unsupported operand type(s) for -: 'list' and 'list'

>Solution :

Using numpy.diff(list) should address the disered output, like this:

import numpy
import matplotlib.pyplot as plt

x1 = [ 0.5, 0.6, 0.3, 0.2 ]
x2 = [ 0.5, 0.6, 0.3, 0.2 ]
x3 = [ 0.2, 0.3, 0.5, 0.6 ]
x4 = [ 0.4, 0.3, 0.2, 0.1 ]

y1 = [ 0.7, 0.9, 0.1, 0.2 ]
y2 = [ 0.8, 0.5, 0.9, 0.2 ]
y3 = [ 0.6, 0.9, 0.1, 0.2 ]
y4 = [ 0.8, 0.2, 0.3, 0.5 ]

deltaX1, deltaX2, deltaX3, deltaX4 = [numpy.diff(x) for x in [x1, x2, x3, x4]]
deltaY1, deltaY2, deltaY3, deltaY4 = [numpy.diff(y) for y in [y1, y2, y3, y4]]

line1 = plt.plot(x1, y1,'bo-',label='apple') 
line2 = plt.plot(x2, y2,'go-',label='banana') 
line3 = plt.plot(x3, y3,'ko-',label='orange')
line4 = plt.plot(x4, y4,'ro-',label='tomato') 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading