jw99
jw99

Reputation: 41

How to exclude x=0 or y=0 values from scatter plot?

I have a scatter graph that looks like this:

enter image description here

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:

enter image description here

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

Answers (3)

niaei
niaei

Reputation: 2399

The problem is you need to exclude both xs and ys where either one is 0.

Imagine you have 10, 0s 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 ys.

What you need?

find x and y rejection masks then apply them to both x an ys:

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

Promila Ghosh
Promila Ghosh

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

Ziur Olpa
Ziur Olpa

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

Related Questions