简体   繁体   English

删除散点图中的点

[英]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 我通过从文本文件导入数据使用python绘制散点图,我想删除x轴值为0的点。这是我写的程序

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. 当我不使用if语句时,这是我的输出 I want to delete all the blue points which have an x axis value of zero. 我想删除所有x轴值为零的蓝点。 Please suggest changes. 请提出修改建议。 If I use the if statement and all the points vanished. 如果我使用if语句并且所有点都消失了。

在此输入图像描述

As your data is stored in numpy arrays you could always just filter them out: 由于您的数据存储在numpy数组中,您可以随时将其过滤掉:

Using either nonzero , or setting some small threshhold value that you filter out: 使用nonzero值或设置过滤掉的一些小阈值:

#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. 并绘制它们而不是原始数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM