TasteofRavioli G
TasteofRavioli G

Reputation: 11

First element not plotting on scatter plot

The first piece of data should be a red point at (3,1.5) yet it doesn't plot, unlike the rest of the points.

data=[[3,1.5,1],
      [2,1,0],
      [4,1.5,1],
      [3,1,0],
      [3.5,.5,1],
      [2,.5,0],
      [5.5,1,1],
      [1,1,0]]
#Data used, only the (3,1.5) part won't graph
#Loop to plot data
for i in range(len(data)):
    point=data[i]
    color="r"
    if point[2]==0:
       color="b"
    pp.axis([0,6,0,6])
    pp.grid()
    pp.scatter(point[0],point[1],c=color)
    scat=pp.figure(3)
scat.show()

Upvotes: 1

Views: 409

Answers (1)

roganjosh
roganjosh

Reputation: 13175

I'm not exactly sure of why you have chosen your current setup, but the issue is the line:

scat=pp.figure(3)

This comes at the end of your for loop... so is defined after the first iteration has basically completed; your first pp.scatter(point[0],point[1],c=color) is negated. Moving scat=pp.figure(3) to the top of your for loop fixes the issue.

Since you're using regular Python lists, I'm not sure they can be sliced cleanly. However, three list comprehensions can be used:

import matplotlib.pyplot as plt

data=[[3, 1.5, 1],
      [2,1,0],
      [4,1.5,1],
      [3,1,0],
      [3.5,.5,1],
      [2,.5,0],
      [5.5,1,1],
      [1,1,0]]

x_data = [item[0] for item in data]
y_data = [item[1] for item in data]
color = ['r' if item[2] else 'b' for item in data]

plt.axis([0,6,0,6])
plt.grid()
plt.scatter(x_data, y_data, color=color)
plt.show() 

Upvotes: 1

Related Questions