在 matplotlib 中映射六边形网格

2023-07-24Python开发问题
23

本文介绍了在 matplotlib 中映射六边形网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想画一个带有六边形网格的图形.最终结果应该看起来像一个蜂窝.但是,我无法使用 matplotlib.collections.RegularPolyCollection 正确调整六边形的大小.谁能看到我做错了什么,或提供另一种解决方案.我想这已经完成了,所以我不需要重新发明轮子.

import matplotlib.pyplot as plt从 matplotlib 导入集合,转换从 matplotlib.colors 导入颜色转换器将 numpy 导入为 np# 做一些偏移,这里为了简单起见做 4 个多边形xyo = [(0,0), (1,0), (0,1), (1,1)]# 六边形边长六边形 = 1# 六边形外接圆的面积circ_area = np.pi * hexside ** 2无花果,斧头 = plt.subplots(1,1)col = collections.RegularPolyCollection(6, np.radians(90), sizes = (circ_area,),偏移量=xyo,transOffset=ax.transData)ax.add_collection(col, autolim=True)颜色 = [colorConverter.to_rgba(c) for c in ('r','g','b','c')]col.set_color(颜色)ax.autoscale_view()plt.show()

解决方案

谁在 2020+ 年遇到同样的问题,请查看我的

安装:

'>>点安装 hexalattice'

高级功能

该模块允许堆叠少量网格、围绕其中心任意旋转网格、对六边形之间的间隙进行高级控制等.

例子:

hex_grid1, h_ax = create_hex_grid(nx=50,ny=50,旋转度= 0,min_diam=1,crop_circ=20,do_plot=真)create_hex_grid(nx=50,ny=50,min_diam=1,旋转度=5,crop_circ=20,do_plot=真,h_ax=h_ax)

I'm wanting to draw a figure with a hexagonal grid. The end result should look like a honeycomb. However, I'm having trouble getting my hexagons sized correctly using matplotlib.collections.RegularPolyCollection. Can anyone see what I am doing wrong, or offer another solution. I imagine this has been done before, so no need for me to reinvent the wheel.

import matplotlib.pyplot as plt
from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
import numpy as np

# Make some offsets, doing 4 polygons for simplicity here
xyo = [(0,0), (1,0), (0,1), (1,1)]
# length of hexagon side
hexside = 1
# area of circle circumscribing the hexagon
circ_area = np.pi * hexside ** 2

fig, ax = plt.subplots(1,1)
col = collections.RegularPolyCollection(6, np.radians(90), sizes = (circ_area,),
    offsets=xyo,transOffset=ax.transData)
ax.add_collection(col, autolim=True)
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c')]
col.set_color(colors)
ax.autoscale_view()
plt.show()

解决方案

Whoever struggles with the same issue in 2020+, check out my hexalattice module: It allows to create hexagonal grids (hexagonal lattices) in 2D with fine control over spatial distribution of the hexagons, circular clop of the lattice and rotations around the central slot.

Usage and graphical output:

from hexalattice.hexalattice import *
hex_centers, _ = create_hex_grid(nx=10,
                                 ny=10,
                                 do_plot=True)
plt.show()    # import matplotlib.pyplot as plt

Installation:

'>> pip install hexalattice'

Advanced features

The module allows stacking of few grids, arbitrary grid rotation around its center, advanced control over gaps between the hexagons etc.

Example:

hex_grid1, h_ax = create_hex_grid(nx=50,
                              ny=50,
                              rotate_deg=0,
                              min_diam=1,
                              crop_circ=20,
                              do_plot=True)
create_hex_grid(nx=50,
                ny=50,
                min_diam=1,
                rotate_deg=5,
                crop_circ=20,
                do_plot=True,
                h_ax=h_ax)

这篇关于在 matplotlib 中映射六边形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10