在 Python 中生成、填充和绘制六边形点阵 [英] Generate, fill and plot a hexagonal lattice in Python

查看:137
本文介绍了在 Python 中生成、填充和绘制六边形点阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改我在方形格子上运行的 Python 脚本(它是基于代理的生物学模型),以在六边形宇宙中工作.

I'd like to modify a Python script of mine operating on a square lattice (it's an agent based model for biology), to work in a hexagonal universe.

这就是我在方形模型中创建和初始化二维矩阵的方式:基本上,N 是晶格的大小,R 给出了我需要在算法开始时更改值的矩阵部分的半径:

This is how I create and initialize the 2D matrix in the square model: basically, N is the size of the lattice and R gives the radius of the part of the matrix where I need to change value at the beginning of the algorithm:

a = np.zeros(shape=(N,N))
center = N/2

for i in xrange(N):
    for j in xrange(N):
        if( ( pow((i-center),2) + pow((j-center),2) ) < pow(R,2) ):
            a[i,j] = 1

然后我让矩阵按照一定的规则进化,最后通过创建一个pickle文件来打印:

I then let the matrix evolve according to certains rules and finally print via the creation of a pickle file:

name = "{0}-{1}-{2}-{3}-{4}.pickle".format(R, A1, A2, B1, B2)
pickle.dump(a, open(name,"w"))

现在,我想在六边形点阵上做完全相同的事情.我读了 this 有趣的 StackOverflow 问题,它阐明了如何用三个坐标,但据我所知,有几件事仍然模糊不清,即

Now, I'd like to do exactly the same but on an hexagonal lattice. I read this interesting StackOverflow question which clearified how to represent the positions on a hexagonal lattice with three coordinates, but a couple of things stay obscure to my knowledge, i.e.

(a) 我应该如何处理Python中的三个轴,考虑到我想要的不等价于一个3D矩阵,由于坐标的限制,以及

(a) how should I deal with the three axes in Python, considering that what I want is not equivalent to a 3D matrix, due to the constraints on the coordinates, and

(b) 如何绘制它?

至于 (a),这就是我想要做的:

As for (a), this is what I was trying to do:

a = np.zeros(shape=(N,N,N))

for i in xrange(N/2-R, N/2+R+1):
    for j in xrange(N/2-R, N/2+R+1):
        for k in xrange(N/2-R, N/2+R+1):
            if((abs(i)+abs(j)+abs(k))/2 <= 3*N/4+R/2):
                a[i,j,k] = 1

在我看来,像这样初始化一个 NxNxN 矩阵,然后找到一种方法来根据坐标上的约束打印它的一个子集,这似乎很复杂.我正在寻找一种更简单的方法,更重要的是,为了了解如何绘制由算法产生的六边形点阵(对此没有任何线索,我暂时没有尝试任何东西).

It seems to me pretty convoluted to initialize a NxNxN matrix like that and then find a way to print a subset of it according to the constraints over the coordinates. I'm looking for a simpler way and, more importantly, for understanding how to plot the hexagonal lattice resulting from the algorithm (no clue on that, I haven't tried anything for the moment).

推荐答案

我同意将六边形晶格硬塞进立方体是有问题的.我的建议是使用通用方案 - 将相邻站点表示为 graph.这适用于 pythons 字典对象,并且在您提供的链接之一中实现轴坐标方案"是微不足道的.这是一个使用 networkx 创建和绘制晶格"的示例.

I agree that trying to shoehorn a hexagonal lattice into a cubic is problematic. My suggestion is to use a general scheme - represent the neighboring sites as a graph. This works very well with pythons dictionary object and it trivial to implement the "axial coordinate scheme" in one of the links you provided. Here is an example that creates and draws the "lattice" using networkx.

import networkx as nx
G = nx.Graph(directed=False)
G.add_node((0,0))

for n in xrange(4):
    for (q,r) in G.nodes(): 
        G.add_edge((q,r),(q,r-1))
        G.add_edge((q,r),(q-1,r))
        G.add_edge((q,r),(q-1,r+1))
        G.add_edge((q,r),(q,r+1))
        G.add_edge((q,r),(q+1,r-1))
        G.add_edge((q,r),(q+1,r))

pos = nx.graphviz_layout(G,prog="neato")
nx.draw(G,pos,alpha=.75)

import pylab as plt
plt.axis('equal')
plt.show()

这不是最佳实现,但它可以生成任意大的格子:

This is isn't the most optimal implementation but it can generate arbitrarily large lattices:

这篇关于在 Python 中生成、填充和绘制六边形点阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆