如何绘制六边形网格形状的(x,y,z)坐标? [英] How to plot (x,y,z) coordinates in the shape of a hexagonal grid?

查看:772
本文介绍了如何绘制六边形网格形状的(x,y,z)坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有以下坐标和相应的颜色 代表六边形的六边形网格:

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"]]

如何在Python中绘制此图形,以使图形上的点保持该六角形形状?此外,如何代表六边形上的颜色"列表.

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.

有点像:

简单的六角形网格

但是外观并不重要,只需要一个简单的散点图类型可视化就足够了,只是让人们可以看到颜色相对于其他六边形的位置.

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.

推荐答案

您只需要将六边形的(y, z)坐标转换为matplotlib轴上的y笛卡尔坐标.

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)

然后您可以使用matplotlib RegularPolygon 打补丁,或使用 scatter .

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

这篇关于如何绘制六边形网格形状的(x,y,z)坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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