日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python区域增长_Python – 有效地为高密度区域创建密度图,稀疏区域的点

發(fā)布時間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python区域增长_Python – 有效地为高密度区域创建密度图,稀疏区域的点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我需要制作一個劇情,就像劇情中高密度地區(qū)的密度圖一樣,但低于某個閾值則使用個別點.我找不到任何與matplotlib縮略圖庫或谷歌搜索中所需的代碼類似的代碼.我有一個我自己寫的工作代碼,但是當點/倉數(shù)量很大時,它有點棘手,(更重要的是)花費了不可接受的長時間.這是代碼:

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')

此代碼生成的圖像如下所示:

但是,當使用較大的數(shù)據(jù)集時,程序需要幾秒鐘到幾分鐘.有什么想法如何加速?謝謝!

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的python区域增长_Python – 有效地为高密度区域创建密度图,稀疏区域的点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。