Python Matplotlib RegularPolyCollection是否具有静态(类似数据)大小?
是否可以创建具有静态大小的RegularPolyCollection 我想用数据单位给出尺寸,而不是屏幕单位。就像越位赛一样 目标是拥有一个直径为9.5毫米的1440个六边形像素的相机图像 可以通过在1440个多边形上循环来实现这一点,但我没有成功地使用PolyCollection创建它,它在创建彩色贴图等方面有很大的优势 以下是我用来绘制1440个六边形的代码(静态大小):Python Matplotlib RegularPolyCollection是否具有静态(类似数据)大小?,python,matplotlib,Python,Matplotlib,是否可以创建具有静态大小的RegularPolyCollection 我想用数据单位给出尺寸,而不是屏幕单位。就像越位赛一样 目标是拥有一个直径为9.5毫米的1440个六边形像素的相机图像 可以通过在1440个多边形上循环来实现这一点,但我没有成功地使用PolyCollection创建它,它在创建彩色贴图等方面有很大的优势 以下是我用来绘制1440个六边形的代码(静态大小): for c, x, y in zip(pixel_color, pixel_x, pixel_y): ax.ad
for c, x, y in zip(pixel_color, pixel_x, pixel_y):
ax.add_artist(
RegularPolygon(
xy=(x, y),
numVertices=6,
radius=4.75,
orientation=0.,
facecolor=c,
edgecolor=edgecolor,
linewidth=1.5,
)
)
此代码生成相同但错误且非静态(就数据而言)的大小:
如何利用集合的优势实现六边形的静态大小?我最近遇到了同样的问题。解决方案是简单地使用
PatchCollection
而不是RegularPolyCollection
。但是,缺点是您必须手动实例化每个补丁。下面是一个在规则网格上绘制10000个规则六边形的代码示例
# imports
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np
# set up figure
fig, ax = plt.subplots(1)
# positions
pixel_x, pixel_y = np.indices((100, 100))
pixel_color = np.random.random_sample(30000).reshape(10000, 3)
dx = 4 # horizontal stride
dy = 5 # vertical stride
# set static radius
poly_radius = 2.5
# list to hold patches
patch_list = []
# creat the patches
for c, x, y in zip(pixel_color, pixel_x.flat, pixel_y.flat):
patch_list.append(
RegularPolygon(
xy=(x*dy, y*dy),
numVertices=6,
radius=poly_radius,
orientation=0.,
facecolor=c,
edgecolor='k'
)
)
pc = PatchCollection(patch_list, match_original=True)
ax.add_collection(pc)
ax.axis([-3, 480, -3, 480])
plt.show()
在我的机器上,这段代码大约需要2.8秒来渲染所有内容
如果您想使用
RegularPolyCollection
,我已经找到了正确设置大小的方法。主要限制是大小取决于轴变换,因此在计算大小之前,需要锁定轴限制和地物大小
在下面的版本中,图形和轴也必须是正方形
将numpy导入为np
将matplotlib导入为mpl
将matplotlib.pyplot作为plt导入
sin60=np.sin(np.pi/3)
图,ax=plt.子批次()
图设置尺寸英寸(8,8)
ax.set_方面(1)
ax.set_xlim(-1.5*sin60,+1.5*sin60)
ax.set_ylim(-1.5*sin60,+1.5*sin60)
ax.设置帧为on(假)
ax.set_xticks([])
ax.set_-yticks([])
坐标=[-1/2,+sin60/2],+1/2,+sin60/2],[0,-sin60/2]]
半径=.5/60
data_to_pixels=ax.transData.get_matrix()[0,0]
像素点到点=1/图获取dpi()*72。
大小=np.pi*(数据到像素*像素到点*半径)**2
hexes=mpl.collections.RegularPolyCollection(
numsides=6,
尺寸=3*(尺寸,),
偏移量=坐标,
edgecolors=3*('k',),
线宽=1,
transOffset=ax.transData)
ax.添加_集合(十六进制)
# imports
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np
# set up figure
fig, ax = plt.subplots(1)
# positions
pixel_x, pixel_y = np.indices((100, 100))
pixel_color = np.random.random_sample(30000).reshape(10000, 3)
dx = 4 # horizontal stride
dy = 5 # vertical stride
# set static radius
poly_radius = 2.5
# list to hold patches
patch_list = []
# creat the patches
for c, x, y in zip(pixel_color, pixel_x.flat, pixel_y.flat):
patch_list.append(
RegularPolygon(
xy=(x*dy, y*dy),
numVertices=6,
radius=poly_radius,
orientation=0.,
facecolor=c,
edgecolor='k'
)
)
pc = PatchCollection(patch_list, match_original=True)
ax.add_collection(pc)
ax.axis([-3, 480, -3, 480])
plt.show()