作者:mobiledu2502897297 | 来源:互联网 | 2023-01-19 11:40
我目前正在尝试在Matplotlib中制作气泡图,其中气泡不重叠,因此将圆圈/气泡打包在图表中,大致类似于此.
我认为可能有用:
使用x = 1,y = 1绘制第一个数据点
通过计算给定标量值的气泡半径来随机绘制其他数据点,以避免重叠
但我无法真正实现它,也无法找到任何相关信息.
1> ImportanceOf..:
以下是一种蛮力方法.
您可以先将所有圆放在网格上,网格间距可以是任意圆的最大半径的两倍.
然后你让圆圈随机行走,并检查每一步中是否有一堆二氧化碳的"势能"变小,并且获得的位置是否有效(即没有重叠).
if (e
作为"潜力",我们可以简单地使用方形径向函数.
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
由于这种随机游走性质,找到解决方案需要一点时间(在这种情况下约为10秒); 您当然可以使用参数(主要1000*self.N
是解决方案修复之前的步骤数),看看哪些适合您的需求.