Reputation: 41
I have a scatter graph that looks like this:
I'm trying to make it so that if x=0 or y=0 then that scatter point is ignored, and only the values where x and y are both greater than or equal to zero are taken:
Here's what I've tried so far:
x = df['tp']
y = df['tp1']
x = x[x > 0.0]
y = y[y > 0.0]
plt.scatter(x,y)
To which I get met with the following:
ValueError: x and y must be the same size
Is there a reason for why this isn't working, or a different solution that I could use?
Upvotes: 1
Views: 2664
Reputation: 2399
The problem is you need to exclude both x
s and y
s where either one is 0
.
Imagine you have 10, 0
s in x
and 5 in y
. Your x
and y
size would not be the same. And more importantly you'll have wrong pairs of x
and y
s.
What you need?
find x
and y
rejection masks then apply them to both x
an y
s:
x = df['tp']
y = df['tp1']
xy_mask = x > 0.0 and y > 0.0
x = x[xy_mask]
y = y[xy_mask]
plt.scatter(x,y)
Upvotes: 2
Reputation: 389
A similar answer is already given in this link. I hope it will help you out.
Not plotting 'zero' in matplotlib or change zero to None [Python]
Upvotes: 0
Reputation: 2102
You are changing the context of the filter while filtering, but you can do:
x_filtered = x[ (x > 0.0) and (y> 0.0)]
y_filtered = y[ (x > 0.0) and (y> 0.0)]
plt.scatter(x_filtered ,y_filtered)
Upvotes: 2