2016-07-26 76 views
1

我繪製使用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(); 

程序這是我的當我不使用if語句時輸出graph。 我想刪除x軸值爲零的所有藍色點。請建議更改。如果我使用if語句並且所有的點都消失了。

enter image description here

+0

而不是刪除點一旦繪製你最好的選擇是根本不要將這些值放在首位。即在mat1 [:,2],mat1 [:,9]上過濾,因爲這些列是藍點的來源。 – miraculixx

回答

3

隨着數據存儲在numpy陣列,你總是可以只篩選出來:

使用這兩種nonzero,或設置你過濾掉一些小的門限值:

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

然後,您可以在受影響的陣列上使用該篩選器:

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

並繪製它們而不是原始數組。

+0

工作。非常感謝你的回答! –

+0

@MahathiChavali很高興你知道了 –