- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我目前正在尝试在 Matplotlib 中制作气泡图,其中气泡不重叠,因此将圆圈/气泡打包在图表中,大约类似于 this .
我认为可能有用的:
但我一直没能真正实现它,也找不到任何相关信息。
最佳答案
以下将是一种蛮力方法。
您可以先将所有圆圈放在一个网格上,网格间距是任何圆圈最大半径的两倍。
然后让圆圈随机游走,并在每一步检查一束圆圈的“势能”是否变小以及获得的位置是否有效(即没有重叠)。
if (e < self.E and self.isvalid(i)):
作为“势”,我们可以简单地使用平方径向函数。
self.p = lambda x,y: np.sum((x**2+y**2)**2)
代码:
import numpy as np
import matplotlib.pyplot as plt
# create 10 circles with different radii
r = np.random.randint(5,15, size=10)
class C():
def __init__(self,r):
self.N = len(r)
self.x = np.ones((self.N,3))
self.x[:,2] = r
maxstep = 2*self.x[:,2].max()
length = np.ceil(np.sqrt(self.N))
grid = np.arange(0,length*maxstep,maxstep)
gx,gy = np.meshgrid(grid,grid)
self.x[:,0] = gx.flatten()[:self.N]
self.x[:,1] = gy.flatten()[:self.N]
self.x[:,:2] = self.x[:,:2] - np.mean(self.x[:,:2], axis=0)
self.step = self.x[:,2].min()
self.p = lambda x,y: np.sum((x**2+y**2)**2)
self.E = self.energy()
self.iter = 1.
def minimize(self):
while self.iter < 1000*self.N:
for i in range(self.N):
rand = np.random.randn(2)*self.step/self.iter
self.x[i,:2] += rand
e = self.energy()
if (e < self.E and self.isvalid(i)):
self.E = e
self.iter = 1.
else:
self.x[i,:2] -= rand
self.iter += 1.
def energy(self):
return self.p(self.x[:,0], self.x[:,1])
def distance(self,x1,x2):
return np.sqrt((x1[0]-x2[0])**2+(x1[1]-x2[1])**2)-x1[2]-x2[2]
def isvalid(self, i):
for j in range(self.N):
if i!=j:
if self.distance(self.x[i,:], self.x[j,:]) < 0:
return False
return True
def plot(self, ax):
for i in range(self.N):
circ = plt.Circle(self.x[i,:2],self.x[i,2] )
ax.add_patch(circ)
c = C(r)
fig, ax = plt.subplots(subplot_kw=dict(aspect="equal"))
ax.axis("off")
c.minimize()
c.plot(ax)
ax.relim()
ax.autoscale_view()
plt.show()
由于其随机游走的性质,找到解决方案需要一点时间(在本例中约为 10 秒);您当然可以使用参数(主要是步数 1000*self.N
直到解决方案被修复)并查看适合您的需求。
关于python - Matplotlib制作不重叠气泡图(圆圈堆积),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131572/
我使用 Dojo fadeIn 和 fadeOut 以及一个计时器旋转了三个堆叠图像。只有最后一个的 href 可用。是否也可以旋转 href? 这是它的 CSS: #main-slides
给定一个 numpy 数组,我想总结统一的元素 block 以形成一个新的、更小的数组。它与分箱类似,但不是按频率分箱。除了通过示例(如下)之外,我不确定如何描述它。 问题:是否有用于此的函数或更清晰
我正在尝试实现某种按钮控制的幻灯片放映,其中包括用于页面顶部全 Angular 图片的 div,用于页面顶部的 div页面底部的另一张全 Angular 图片和中央内容的最终 div(包括控制“幻
嘿,我正在使用 D3JS 作为图表库,我真的很想利用气泡图中的超酷功能。上主D3JS chart站点下面的Bubble Chart用来比较两组数据: Bubble Chart . 我想知道是否有人真的
我是一名优秀的程序员,十分优秀!