|
假設(shè)我一直在我的筆記本電腦上用3g調(diào)制解調(diào)器和GPS驅(qū)動(dòng)設(shè)定路線,而我家里的電腦記錄了ping延遲.我已將ping與GPS lat / long相關(guān)聯(lián),現(xiàn)在我想將這些數(shù)據(jù)可視化.
我每天有大約80,000個(gè)數(shù)據(jù)點(diǎn),我想顯示幾個(gè)月的價(jià)值.我特別感興趣的是顯示ping持續(xù)超時(shí)的區(qū)域(即ping == 1000).
散點(diǎn)圖
我的第一次嘗試是使用散點(diǎn)圖,每個(gè)數(shù)據(jù)輸入一個(gè)點(diǎn).如果超時(shí),我將點(diǎn)的大小增加了5倍,因此很明顯這些區(qū)域在哪里.我也將alpha降為0.1,以粗略的方式看到重疊點(diǎn).
# Colour
c = pings
# Size
s = [2 if ping < 1000 else 10 for ping in pings]
# Scatter plot
plt.scatter(longs, lats, s=s, marker='o', c=c, cmap=cm.jet, edgecolors='none', alpha=0.1)
顯而易見的問題是它每個(gè)數(shù)據(jù)點(diǎn)顯示一個(gè)標(biāo)記,這是顯示大量數(shù)據(jù)的一種非常差的方法.如果我經(jīng)過兩次相同的區(qū)域,那么第一遍數(shù)據(jù)就會(huì)顯示在第二遍的頂部.
在均勻網(wǎng)格上插值
然后我嘗試使用numpy和scipy在偶數(shù)網(wǎng)格上進(jìn)行插值.
# Convert python list to np arrays
x = np.array(longs, dtype=float)
y = np.array(lats, dtype=float)
z = np.array(pings, dtype=float)
# Make even grid (200 rows/cols)
xi = np.linspace(min(longs), max(longs), 200)
yi = np.linspace(min(lats), max(lats), 200)
# Interpolate data points to grid
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear', fill_value=0)
# Plot contour map
plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
從this example起
這看起來很有趣(很多顏色和形狀),但它在我沒有探索過的區(qū)域外推得太遠(yuǎn)了.你看不到我走過的路線,只看到紅/藍(lán)斑點(diǎn).
如果我在一條大曲線上行駛,它將插入兩者之間的區(qū)域(見下文):
插入不均勻的網(wǎng)格
然后我嘗試使用meshgrid(xi,yi = np.meshgrid(lats,longs))而不是固定網(wǎng)格,但我被告知我的數(shù)組太大了.
有沒有一種簡(jiǎn)單的方法可以從我的觀點(diǎn)創(chuàng)建一個(gè)網(wǎng)格?
我的要求:
>處理大型數(shù)據(jù)集(80,000 x 60 =?5m點(diǎn)) >通過平均(我假設(shè)插值將執(zhí)行此操作)或通過為每個(gè)點(diǎn)取最小值來顯示每個(gè)點(diǎn)的重復(fù)數(shù)據(jù). >不要過分推斷數(shù)據(jù)點(diǎn)
我很滿意散點(diǎn)圖(頂部),但我需要一些方法來平均數(shù)據(jù)才能顯示它.
(對(duì)于狡猾的mspaint圖紙道歉,我無法上傳實(shí)際數(shù)據(jù))
解:
# Get sum
hsum, long_range, lat_range = np.histogram2d(longs, lats, bins=(res_long,res_lat), range=((a,b),(c,d)), weights=pings)
# Get count
hcount, ignore1, ignore2 = np.histogram2d(longs, lats, bins=(res_long,res_lat), range=((a,b),(c,d)))
# Get average
h = hsum/hcount
x, y = np.where(h)
average = h[x, y]
# Make scatter plot
scatterplot = ax.scatter(long_range[x], lat_range[y], s=3, c=average, linewidths=0, cmap="jet", vmin=0, vmax=1000)
解決方法: 為了簡(jiǎn)化您的問題,您有兩組點(diǎn),一組用于ping< 1000,一組用于ping> = 1000. 由于點(diǎn)數(shù)非常大,因此無法通過scatter()直接繪制它們.我創(chuàng)建了一些示例數(shù)據(jù):
longs = (np.random.rand(60, 1) np.linspace(-np.pi, np.pi, 80000)).reshape(-1)
lats = np.sin(longs) np.random.rand(len(longs)) * 0.1
bad_index = (longs>0) & (longs<1)
bad_longs = longs[bad_index]
bad_lats = lats[bad_index]
(longs,lats)是ping< 1000的點(diǎn),(bad_longs,bad_lats)是ping> 1000的點(diǎn)
您可以使用numpy.histogram2d()來計(jì)算點(diǎn)數(shù):
ranges = [[np.min(lats), np.max(lats)], [np.min(longs), np.max(longs)]]
h, lat_range, long_range = np.histogram2d(lats, longs, bins=(400,400), range=ranges)
bad_h, lat_range2, long_range2 = np.histogram2d(bad_lats, bad_longs, bins=(400,400), range=ranges)
h和bad_h是每個(gè)小區(qū)域中的點(diǎn)數(shù).
然后,您可以選擇許多方法來可視化它.例如,您可以通過scatter()繪制它:
y, x = np.where(h)
count = h[y, x]
pl.scatter(long_range[x], lat_range[y], s=count/20, c=count, linewidths=0, cmap="Blues")
count = bad_h[y, x]
pl.scatter(long_range2[x], lat_range2[y], s=count/20, c=count, linewidths=0, cmap="Reds")
pl.show()
這是完整的代碼:
import numpy as np
import pylab as pl
longs = (np.random.rand(60, 1) np.linspace(-np.pi, np.pi, 80000)).reshape(-1)
lats = np.sin(longs) np.random.rand(len(longs)) * 0.1
bad_index = (longs>0) & (longs<1)
bad_longs = longs[bad_index]
bad_lats = lats[bad_index]
ranges = [[np.min(lats), np.max(lats)], [np.min(longs), np.max(longs)]]
h, lat_range, long_range = np.histogram2d(lats, longs, bins=(300,300), range=ranges)
bad_h, lat_range2, long_range2 = np.histogram2d(bad_lats, bad_longs, bins=(300,300), range=ranges)
y, x = np.where(h)
count = h[y, x]
pl.scatter(long_range[x], lat_range[y], s=count/20, c=count, linewidths=0, cmap="Blues")
count = bad_h[y, x]
pl.scatter(long_range2[x], lat_range2[y], s=count/20, c=count, linewidths=0, cmap="Reds")
pl.show()
輸出數(shù)字是: 來源:https://www./content-1-283101.html
|