anon136
anon136

Reputation: 13

Delete points in scatter plot

I plotted a scatter plot using python by importing data from text files and I want to delete points with x axis values 0. This is the program I have written

mat0 = genfromtxt("herbig0.txt");
mat1 = genfromtxt("coup1.txt");
pyplot.xlim([-2,6])
pyplot.ylim([26,33])
colors=['red', 'blue','green']
pyplot.scatter(mat0[:,13], mat0[:,4], label = "herbig stars", color=colors[0]);
if mat1[:,2] != 0:
pyplot.scatter(mat1[:,2], mat1[:,9], label = "COUP data of SpT F5-M6 ", color=colors[1]);
pyplot.scatter(mat1[:,2], mat1[:,10], label = "COUP data of SpT B0-F5", color=colors[2]);
pyplot.legend();
pyplot.xlabel('Log(Lbol) (sol units)')
pyplot.ylabel('Log(Lx) (erg/s)')
pyplot.title('Lx vs Lbol')
pyplot.show();

This is my output graph when I don't use the if statements. I want to delete all the blue points which have an x axis value of zero. Please suggest changes. If I use the if statement and all the points vanished.

enter image description here

Upvotes: 1

Views: 1390

Answers (1)

M.T
M.T

Reputation: 5231

As your data is stored in numpy arrays you could always just filter them out:

Using either nonzero, or setting some small threshhold value that you filter out:

#Either
mat_filter = np.nonzero(mat1[:,2])
#or
mat_filter = np.abs(mat1[:,2])>1e-12

Then you can use that filter on the affected arrays:

mat1mod2 = mat1[:,2][mat_filter]
mat1mod9 = mat1[:,9][mat_filter]
mat1mod10 = mat1[:,10][mat_filter]

And plot them instead of the original arrays.

Upvotes: 3

Related Questions