假设我在我的笔记本电脑上使用 3g 调制解调器和 GPS 沿着设定的路线行驶,而我在家里的电脑记录了 ping 延迟。我已将 ping 与 GPS 纬度/经度相关联,现在我想可视化此数据。
我每天有大约 80,000 个数据点,我想显示几个月的数据。我特别感兴趣的是显示 ping 持续超时的区域(即 ping == 1000)。
散点图
我的第一次尝试是使用散点图,每个数据条目一个点。如果超时,我将点的大小放大 5 倍,因此这些区域的位置很明显。我还将 alpha 降低到 0.1,以便以粗略的方式查看重叠点。
# 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)
这样做的明显问题是它为每个数据点显示一个标记,这是显示大量数据的一种非常糟糕的方式。如果我两次开车经过同一区域,那么第一次通过的数据只会显示在第二次通过的顶部。
在偶数网格上插值
然后我尝试使用 numpy 和 scipy 在偶数网格上进行插值。
# 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
这看起来很有趣(很多颜色和形状),但它在我尚未探索的区域周围推断得太远了。你看不到我走过的路线,只有红色/蓝色的 Blob 。
如果我在一条大弯道上行驶,它会在(见下文)之间的区域进行插值:
在不均匀的网格上插值
然后我尝试使用 meshgrid (xi, yi = np.meshgrid(lats, longs)
) 而不是固定网格,但我被告知我的数组太大了。
有没有一种简单的方法可以根据我的点创建网格?
我的要求:
- 处理大型数据集(80,000 x 60 = ~5m 点)
- 通过平均(我假设插值会这样做)或为每个点取最小值来显示每个点的重复数据。
- 不要从数据点推断太远
我对散点图(顶部)很满意,但我需要一些方法在显示数据之前对其进行平均。
(抱歉 mspaint 画得不好,我无法上传实际数据)
解决方案:
# 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)
最佳答案
为了简化您的问题,您有两组点,一组用于 ping<1000,一组用于 ping>=1000。 由于点数非常多,您不能直接通过 scatter() 绘制它们。我通过以下方式创建了一些示例数据:
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 点,(bad_longs, bad_lats) 是 ping>1000 点
您可以使用 numpy.histogram2d() 来计算点数:
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 是每个小方 block 区域的点数。
然后你可以选择很多方法来可视化它。例如,您可以通过 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()
输出图为:
关于python - 3g 覆盖 map - 可视化纬度、经度、ping 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9205104/