在 Python 中将占用率高的区域绘制为密度图或等高线
Plot areas of high occupancy as a densitymap or contour in Python
我有一张包含多个点的图像,我给它们贴上了标签,这样我就可以知道它们的坐标 (x,y)。
现在我有一个坐标列表,例如:
obj [0]: (1.5918367346938775, 806.42857142857144)
obj [1]: (5.5131578947368425, 860.59539473684208)
obj [2]: (0.0, 853.0)
(...)
obj [1183]: (1722.6078431372548, 1575.8725490196077)
obj [1184]: (1725.7272727272727, 330.72727272727275)
obj [1185]: (1726.4285714285713, 335.85714285714283)
obj [1186]: (1727.0, 327.0)
拥有大量的 (x,y) 点位置数据集,我想在 Python 中将高占用区域绘制为密度图或等高线。
我使用了 matplotlib 的等高线,但它没有给我很好的密度信息:
import matplotlib.pyplot as plt
import numpy as np
z = th1
plt.subplot(2,2,1),plt.contour(np.transpose(z))
plt.title('Basic Contour')[![enter image description here][1]][1]
您尝试过在直方图中对它们进行分箱吗?
import numpy as np
import matplotlib.pyplot as plt
coords = np.random.normal(0, 1, (2, 1000))
H, xedges, yedges = np.histogram2d(coords[0], coords[1], bins=50)
im = plt.imshow(H, interpolation='nearest', origin='low',
extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
估计密度的一种方法是使用matplotlib.tri.Triangulation
class,它使用三角形连接点,然后使用基于坐标的解析公式计算每个三角形的面积。然后可以从每个三角形表面的倒数推导出密度。
import numpy as np
from matplotlib.pyplot import (tripcolor, triplot, scatter,
show, title, savefig, colorbar)
from matplotlib.tri import Triangulation, TriAnalyzer
# Coordinates
x = np.random.random(100)
y = np.random.random(100)
# Triangulation
tri = Triangulation(x, y)
# Remove flat triangles
mask = TriAnalyzer(tri).get_flat_tri_mask(0.01)
tri.set_mask(mask)
# Coordinates of the edges
ii1, ii2, ii3 = tri.triangles.T
x1 = x[ii1] ; y1 = y[ii1]
x2 = x[ii2] ; y2 = y[ii2]
x3 = x[ii3] ; y3 = y[ii3]
# Surfaces
surf = 0.5*np.abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))
# Density
dens = 1/(surf*3) # 3 points per triangle!
# Plot
xd = (x1+x2+x3)/3.
yd = (y1+y2+y3)/3.
tripcolor(xd, yd, dens, cmap='cool')
colorbar()
triplot(tri, color='k', linewidth=0.3)
scatter(x,y)
title('Density')
savefig('density.png')
show()
我有一张包含多个点的图像,我给它们贴上了标签,这样我就可以知道它们的坐标 (x,y)。 现在我有一个坐标列表,例如:
obj [0]: (1.5918367346938775, 806.42857142857144)
obj [1]: (5.5131578947368425, 860.59539473684208)
obj [2]: (0.0, 853.0)
(...)
obj [1183]: (1722.6078431372548, 1575.8725490196077)
obj [1184]: (1725.7272727272727, 330.72727272727275)
obj [1185]: (1726.4285714285713, 335.85714285714283)
obj [1186]: (1727.0, 327.0)
拥有大量的 (x,y) 点位置数据集,我想在 Python 中将高占用区域绘制为密度图或等高线。
我使用了 matplotlib 的等高线,但它没有给我很好的密度信息:
import matplotlib.pyplot as plt
import numpy as np
z = th1
plt.subplot(2,2,1),plt.contour(np.transpose(z))
plt.title('Basic Contour')[![enter image description here][1]][1]
您尝试过在直方图中对它们进行分箱吗?
import numpy as np
import matplotlib.pyplot as plt
coords = np.random.normal(0, 1, (2, 1000))
H, xedges, yedges = np.histogram2d(coords[0], coords[1], bins=50)
im = plt.imshow(H, interpolation='nearest', origin='low',
extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
估计密度的一种方法是使用matplotlib.tri.Triangulation
class,它使用三角形连接点,然后使用基于坐标的解析公式计算每个三角形的面积。然后可以从每个三角形表面的倒数推导出密度。
import numpy as np
from matplotlib.pyplot import (tripcolor, triplot, scatter,
show, title, savefig, colorbar)
from matplotlib.tri import Triangulation, TriAnalyzer
# Coordinates
x = np.random.random(100)
y = np.random.random(100)
# Triangulation
tri = Triangulation(x, y)
# Remove flat triangles
mask = TriAnalyzer(tri).get_flat_tri_mask(0.01)
tri.set_mask(mask)
# Coordinates of the edges
ii1, ii2, ii3 = tri.triangles.T
x1 = x[ii1] ; y1 = y[ii1]
x2 = x[ii2] ; y2 = y[ii2]
x3 = x[ii3] ; y3 = y[ii3]
# Surfaces
surf = 0.5*np.abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))
# Density
dens = 1/(surf*3) # 3 points per triangle!
# Plot
xd = (x1+x2+x3)/3.
yd = (y1+y2+y3)/3.
tripcolor(xd, yd, dens, cmap='cool')
colorbar()
triplot(tri, color='k', linewidth=0.3)
scatter(x,y)
title('Density')
savefig('density.png')
show()