My code is skipping the 3rd and 4th elif statement. Instead of plotting red or purple dots for values that fall between 86 and 106 and then values between 106 and 201, they are plotting maroon which is the else statement. Why is my code skipping over those two conditions? It reads the first two elif statements as I wish so I do no understand what is happening with the last two.
if zlist < '55':
plt.scatter(x = xlist, y = ylist, color = 'green')
plt.xticks(rotation=90)
plt.text(xlist, ylist, zlist)
elif '55' < zlist < '71':
plt.scatter(x =xlist, y =ylist, color = 'yellow')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif '71' < zlist < '86':
plt.scatter(x =xlist, y =ylist, color = 'orange')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif '86' < zlist < '106':
plt.scatter(x =xlist, y =ylist, color = 'red')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif '106' < zlist < '201':
plt.scatter(x =xlist, y =ylist, color = 'purple')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif zlist > '201':
plt.scatter(x =xlist, y =ylist, color = 'maroon')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
CodePudding user response:
zlist = int(zlist)
if zlist < 55:
plt.scatter(x = xlist, y = ylist, color = 'green')
plt.xticks(rotation=90)
plt.text(xlist, ylist, zlist)
elif 55 < zlist <= 71:
plt.scatter(x =xlist, y =ylist, color = 'yellow')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif 71 < zlist <= 86:
plt.scatter(x =xlist, y =ylist, color = 'orange')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif 86 < zlist <= 106:
plt.scatter(x =xlist, y =ylist, color = 'red')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif 106 < zlist <= 201:
plt.scatter(x =xlist, y =ylist, color = 'purple')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
elif zlist > 201:
plt.scatter(x =xlist, y =ylist, color = 'maroon')
plt.xticks(rotation = 90)
plt.text(xlist, ylist, zlist)
You are attempting to use comparisons on strings, but Python won't perform numeric comparisons on strings. It will instead attempt to sort/compare them alphabetically, which will result in unexpected behavior. You can fix this by casting Zlist to an integer and performing integer comparisons instead.
You might also want to adjust the elif comparisons to include <= for the second breakpoint. Otherwise, numbers such as 71, 86, 106, 201, etc. won't be matched by any of the elif blocks and will drop straight to the else block.