2016-09-26 57 views
1

我有一個散點圖,我想輸出爲SVG(Python 3.5)。但是,當與agg一起使用作爲後端時,有些點就會丟失。查看數據和PNG和SVG輸出。這是某種錯誤配置還是錯誤?Matplotlib:點不顯示在SVG中

代碼:

import matplotlig 
matplotlib.use('agg') 
import matplotlib.pyplot as plt 

x = [22752.9597858324,33434.3100283611,None,None,3973.2239542398,None,None,None 
,None,None,None,None,None,960.6513071797,None,None,None,None,None,None,None 
,None,None,None,None,None,749470.931292081,None,None,None,None,None,None 
,None,None,None,None,None,None,None,None,23045.262784499,None,None,None 
,None,None,None,None,1390.8383822667,None,None,9802.5632611025 
,3803.3240362092,None,None,None,None,None,2058.1191666219,None 
,3777.5383953988,None,91224.0759036624,23296.1857550166,27956.249381887 
,None,237247.707648005,None,None,None,None,None,None,None,None,None 
,760.3493458787,None,321687.799104496,None,None,22339.5617383239,None,None 
,None,None,None,28135.0261453192,None,None,None,None,None,None,None 
,1687.4387356974,None,None,29037.8494868489,None,None,None,None,None,None 
,None,3937.3066755226,None,None,None,None] 
y = [63557.4319306279,None,None,None,9466.0204228915,None,None,None,None,None 
,None,None,None,3080.3393940948,None,None,None,None,None,None,None,None 
,None,None,None,None,592184.803802073,None,None,None,None,None,None,None 
,None,None,None,None,None,None,None,18098.725166318,None,None,None,None 
,None,None,None,789.2710621298,None,None,7450.9539135753,4251.6033622036 
,None,None,None,None,None,1277.1691956597,None,4273.5950324508,None 
,51861.5572682614,19415.3369388317,2117.2407148378,None,160776.887146683 
,None,None,None,None,None,None,None,None,None,1550.3003177484,None 
,402333.163939038,None,None,16604.3340243551,None,None,None,None,None 
,32545.0784355136,None,None,None,None,None,None,None,2567.9264180605,None 
,None,45786.935597305,None,None,None,None,None,None,None,5645.5218715636 
,None,None,None,None] 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(x, y, '.') 
fig.savefig('/home/me/test_svg', format='svg') 
fig.savefig('/home/me/test_png', format='png') 

結果:

PNG:

PNG output

SVG:

SVG output

回答

0

該問題似乎與None值有關。儘管如果不存在匹配點,那麼根本沒有包含任何點,它似乎會影響SVG的渲染。刪除這兩個條目,如果在其中一個點是None解決了這個問題。

data = np.array([x, y]) 
data = data.transpose() 
# Filter out pairs of points of which at least one is None. 
data = [pair for pair in data if pair[0] and pair[1]] 
data = np.array(data).transpose() 
x = data[0] 
y = data[1] 
ax.plot(x, y, '.') 
fig.savefig('/home/me/test_svg', format='svg') 
fig.savefig('/home/me/test_png', format='png')