六角形的绘制python_如何绘制六边形网格形状的(x,y,z)坐标?

1586010002-jmsa.png

If for example, I have the following coordinates with corresponding colors

which represent a hexagonal shaped grid of hexagons:

coord = [[0,0,0],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0],[1,0,-1]]

colors = [["Green"],["Blue"],["Green"],["Green"],["Red"],["Green"],["Green"]]

How can one plot this in Python so that the points on the plot retain that hexagonal shape? Additionally how can one represent the 'colors' list on the hexagon.

Somewhat like this:

Simple Hexagonal grid

rhoXQ.png

But the look doesn't matter, just a simple scatter plot type visualization would suffice, just so that one can see where in relation to other hexagons the colors lie.

解决方案

You just need to turn the (y, z) coordinates from your hexagons into the y cartesian coordinate on the matplotlib axes.

I think the correct way to do that is using this formula:

y_cartesian = (2 / 3) * sin(60) * (y_hex - z_hex)

You can then add the hexagons using a matplotlib RegularPolygon patch, or plot the centres using scatter.

Here's a script to make a plot from your lists:

import matplotlib.pyplot as plt

from matplotlib.patches import RegularPolygon

import numpy as np

coord = [[0,0,0],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0],[1,0,-1]]

colors = [["Green"],["Blue"],["Green"],["Green"],["Red"],["Green"],["Green"]]

labels = [['yes'],['no'],['yes'],['no'],['yes'],['no'],['no']]

# Horizontal cartesian coords

hcoord = [c[0] for c in coord]

# Vertical cartersian coords

vcoord = [2. * np.sin(np.radians(60)) * (c[1] - c[2]) /3. for c in coord]

fig, ax = plt.subplots(1)

ax.set_aspect('equal')

# Add some coloured hexagons

for x, y, c, l in zip(hcoord, vcoord, colors, labels):

color = c[0].lower() # matplotlib understands lower case words for colours

hex = RegularPolygon((x, y), numVertices=6, radius=2. / 3.,

orientation=np.radians(30),

facecolor=color, alpha=0.2, edgecolor='k')

ax.add_patch(hex)

# Also add a text label

ax.text(x, y+0.2, l[0], ha='center', va='center', size=20)

# Also add scatter points in hexagon centres

ax.scatter(hcoord, vcoord, c=[c[0].lower() for c in colors], alpha=0.5)

plt.show()

asUZx.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Python中,我们可以使用turtle库来绘制六边形,具体的代码如下: ```python import turtle # 创建画布 canvas = turtle.Screen() # 创建画笔 pen = turtle.Turtle() # 绘制六边形 for i in range(6): pen.forward(100) pen.left(60) # 关闭画布 canvas.exitonclick() ``` 这段代码中,我们首先导入了turtle库,然后创建了画布和画笔。接着,我们使用for循环来绘制六边形,每次向前移动100个像素,然后向左转60度。最后,我们关闭了画布。运行代码后,就可以看到绘制出来的六边形了。 ### 回答2: 要在Python绘制六角形,我们可以使用turtle模块。turtle是一个绘图库,可以让我们通过控制一个海龟进行绘图。 首先,我们需要导入turtle模块。在Python中,使用以下代码导入turtle模块: ``` import turtle ``` 接下来,创建一个海龟对象: ``` t = turtle.Turtle() ``` 我们可以通过控制海龟移动和转向来绘制六角形。首先,向前移动一定距离,然后向右转60度,再向前移动一定距离,依此类推。如下所示: ``` for i in range(6): t.forward(100) # 向前移动100距离 t.right(60) # 向右转60度 ``` 最后,我们需要关闭绘图窗口,以显示绘制的图形: ``` turtle.done() ``` 完整的代码如下所示: ``` import turtle t = turtle.Turtle() for i in range(6): t.forward(100) # 向前移动100距离 t.right(60) # 向右转60度 turtle.done() ``` 运行以上代码,就可以在画布上绘制出一个六角形。你可以根据需要调整绘制的大小和颜色等参数。 ### 回答3: 六角形是一种具有六个边和六个角的多边形。可以使用Python编程语言来绘制六角形。在Python中,我们可以使用turtle模块来进行图形绘制。 首先,我们需要导入turtle模块: ```python import turtle ``` 然后,我们可以创建一个turtle对象,并设置绘制的速度和线的颜色: ```python hexagon = turtle.Turtle() hexagon.speed(1) hexagon.color("red") ``` 接下来,我们可以使用循环语句绘制六个边: ```python for _ in range(6): hexagon.forward(100) # 向前移动100个像素 hexagon.right(60) # 向右旋转60度 ``` 最后,我们可以调用turtle.done()函数来保持窗口打开,以便查看绘制的结果: ```python turtle.done() ``` 完整的代码如下所示: ```python import turtle hexagon = turtle.Turtle() hexagon.speed(1) hexagon.color("red") for _ in range(6): hexagon.forward(100) hexagon.right(60) turtle.done() ``` 运行这段代码,我们就可以在窗口中看到绘制六角形。你可以按照需要调整绘制六角形的大小、颜色等属性。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值