2016-05-27 44 views
2

使用矢量後端(pdf,eps)時,文件大小和渲染時間方面的浪費會讓其他點完全遮擋。這些如何被刪除?刪除遮擋的散點圖

回答

2

這是一個幾乎不公平的問題,因爲這取決於標記尺寸與實際座標是difficult to calculate

在任何情況下,也許一半的解決方案會爲你做。我在想,如果你計算所有點之間的距離,當一個對在給定的容差範圍內時,你只能使用其中一個點(而不是兩個點)。這不會是完美的,但它可能會證明是有用的。簡單的測試,使用這種想法(我希望我得到的距離邏輯右):

import matplotlib.pyplot as plt 
import scipy 

x = np.random.normal(0,1,15000) 
y = np.random.normal(0,1,15000) 
tol = 0.01 

xy = np.hstack((x[:,np.newaxis],y[:,np.newaxis])) 
d = scipy.spatial.distance.cdist(xy,xy) 
b = np.ones(x.shape,dtype='bool') 
for i in range(d.shape[0]-1): 
    if d[i,i+1:].min() < tol and b[i]: 
     b[i+1+d[i,i+1:].argmin()] = False 

x2 = x[b] 
y2 = y[b] 

f, (ax1, ax2) = plt.subplots(1, 2) 

ax1.scatter(x,y,s=90) 
ax1.set_xlim(-6,6) 
ax1.set_ylim(-6,6) 
ax2.scatter(x2,y2,s=90) 
ax2.set_xlim(-6,6) 
ax2.set_ylim(-6,6) 

print('Before: ', x.shape,'\nNow: ',x2.shape) 
plt.show() 

,給了我這樣的結果:

Before: (15000,) 
Now: (13004,) 

Attempting to remove invisible markers from matplotlib plot

代表的節約如果你仔細觀察,你會注意到這並不完美,但我確信在tol論點中的一點校準可以顯着改善情節。