1

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?

jw99
  • 41
  • 6
  • The reason why it isn't working is that x and y are corresponding values, when you filter them separately you break the pairing. You need to zip them together, filter on both conditions and then unzip them back into x and y coords. – matszwecja Mar 30 '22 at 08:26

3 Answers3

2

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)
Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27
2

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)
MSH
  • 1,743
  • 2
  • 14
  • 22
0

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]

Promila Ghosh
  • 389
  • 4
  • 12