Tôi cần tạo một ô có chức năng giống như ô mật độ cho các vùng có mật độ cao trên ô, nhưng dưới một số ngưỡng sử dụng các điểm riêng lẻ. Tôi không thể tìm thấy bất kỳ mã hiện có nào trông giống như những gì tôi cần trong thư viện hình thu nhỏ matplotlib hoặc từ tìm kiếm trên google. Tôi có một mã số làm việc tôi đã viết bản thân mình, nhưng nó là hơi phức tạp và (quan trọng hơn) mất một thời gian dài không thể chấp nhận được khi số lượng các điểm/thùng là lớn. Đây là mã:Tạo một ô mật độ cho các vùng có mật độ cao, điểm cho các vùng thưa thớt
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab
import numpy.random
#Create the colormap:
halfpurples = {'blue': [(0.0,1.0,1.0),(0.000001, 0.78431373834609985, 0.78431373834609985),
(0.25, 0.729411780834198, 0.729411780834198), (0.5,
0.63921570777893066, 0.63921570777893066), (0.75,
0.56078433990478516, 0.56078433990478516), (1.0, 0.49019607901573181,
0.49019607901573181)],
'green': [(0.0,1.0,1.0),(0.000001,
0.60392159223556519, 0.60392159223556519), (0.25,
0.49019607901573181, 0.49019607901573181), (0.5,
0.31764706969261169, 0.31764706969261169), (0.75,
0.15294118225574493, 0.15294118225574493), (1.0, 0.0, 0.0)],
'red': [(0.0,1.0,1.0),(0.000001,
0.61960786581039429, 0.61960786581039429), (0.25,
0.50196081399917603, 0.50196081399917603), (0.5,
0.41568627953529358, 0.41568627953529358), (0.75,
0.32941177487373352, 0.32941177487373352), (1.0,
0.24705882370471954, 0.24705882370471954)]}
halfpurplecmap = mpl.colors.LinearSegmentedColormap('halfpurples',halfpurples,256)
#Create x,y arrays of normally distributed points
npts = 1000
x = numpy.random.standard_normal(npts)
y = numpy.random.standard_normal(npts)
#Set bin numbers in both axes
nxbins = 25
nybins = 25
#Set the cutoff for resolving the individual points
minperbin = 1
#Make the density histrogram
H, yedges, xedges = np.histogram2d(y,x,bins=(nybins,nxbins))
#Reorient the axes
H = H[::-1]
extent = [xedges[0],xedges[-1],yedges[0],yedges[-1]]
#Compute all bins where the density plot value is below (or equal to) the threshold
lowxleftedges = [[xedges[i] for j in range(len(H[:,i])) if H[j,i] <= minperbin] for i in range(len(H[0,:]))]
lowxrightedges = [[xedges[i+1] for j in range(len(H[:,i])) if H[j,i] <= minperbin] for i in range(len(H[0,:]))]
lowyleftedges = [[yedges[-(j+2)] for j in range(len(H[:,i])) if H[j,i] <= minperbin] for i in range(len(H[0,:]))]
lowyrightedges = [[yedges[-(j+1)] for j in range(len(H[:,i])) if H[j,i] <= minperbin] for i in range(len(H[0,:]))]
#Flatten and convert to numpy array
lowxleftedges = np.asarray([item for sublist in lowxleftedges for item in sublist])
lowxrightedges = np.asarray([item for sublist in lowxrightedges for item in sublist])
lowyleftedges = np.asarray([item for sublist in lowyleftedges for item in sublist])
lowyrightedges = np.asarray([item for sublist in lowyrightedges for item in sublist])
#Find all points that lie in these regions
lowdatax = [[x[i] for j in range(len(lowxleftedges)) if lowxleftedges[j] <= x[i] and x[i] <= lowxrightedges[j] and lowyleftedges[j] <= y[i] and y[i] <= lowyrightedges[j]] for i in range(len(x))]
lowdatay = [[y[i] for j in range(len(lowyleftedges)) if lowxleftedges[j] <= x[i] and x[i] <= lowxrightedges[j] and lowyleftedges[j] <= y[i] and y[i] <= lowyrightedges[j]] for i in range(len(y))]
#Flatten and convert into numpy array
lowdatax = np.asarray([item for sublist in lowdatax for item in sublist])
lowdatay = np.asarray([item for sublist in lowdatay for item in sublist])
#Plot
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(lowdatax,lowdatay,linestyle='.',marker='o',mfc='k',mec='k')
cp1 = ax1.imshow(H,interpolation='nearest',extent=extent,cmap=halfpurplecmap,vmin=minperbin)
fig1.colorbar(cp1)
fig1.savefig('contourtest.eps')
Mã này tạo ra một hình ảnh trông như thế này:
Tuy nhiên, khi sử dụng trên dữ liệu lớn hơn thiết lập chương trình phải mất vài giây đến vài phút. Bất kỳ suy nghĩ về làm thế nào để tăng tốc độ này lên? Cảm ơn!
Một vài ngày trước bạn gái của tôi chỉ cho tôi lô đẹp cô đã thực hiện với [ 'smoothScatter'] (http://rfunction.com/archives/595) chức năng R, mà thuận lợi kết hợp một âm mưu phân tán và bản đồ mật độ. Tôi đã ngay lập tức thất vọng rằng không có tương đương trong matplotlib, vì vậy tôi vui mừng khi tìm thấy câu hỏi cũ này ở đây về nó. – Julien