【发布时间】:2019-04-20 11:06:54
【问题描述】:
在下图 (1) 中,六边形网格周围有很多空白区域,我不知道如何删除。我尝试了不同的方法,即tight_layout 等。
代码是
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy
def plot_som_hm(hm,colormap,a=1):
# setup hexagon offsets
m,n = hm.shape
offsety = .75 * 2*a
offsetx = numpy.sqrt(3) * a
evenrow = numpy.sqrt(3)/2 * a
# set up the figure
fig,ax = plt.subplots(figsize=(2,2))
ax.set_aspect('equal')
# define colormap
cmap = cm.ScalarMappable(None,colormap)
norm = colors.Normalize(vmin=hm.min(),vmax=hm.max())
# iterate over the hitmap, drawing a hexagon
xs = []
ys = []
for i in range(m):
for j in range(n):
# calculate center point for current hexagonal grid & draw it
offsetr = evenrow if i % 2 == 0 else 0
x,y = (j*offsetx+offsetr,-i*offsety)
hexg = RegularPolygon(
(x,y),numVertices=6,radius=a,facecolor=cmap.cmap(norm(hm[i][j]))
)
ax.add_patch(hexg)
xs.append(x)
ys.append(y)
# add a scatter plot so all hexagons show up & turn off ticks
ax.scatter(xs,ys,alpha=1.0)
ax.set_xticks([])
ax.set_yticks([])
# add a colorbar
sm = plt.cm.ScalarMappable(cmap=colormap,norm=norm)
sm._A = []
plt.colorbar(sm,ticks=range(int(hm.min()),int(hm.max())+1))
# and show the hitmap
plt.show()
可以被plot_som_hm(hitmap,'inferno',-0.5)调用
我不确定空格是否是调用 subplots (figsize=(2,2)) 或其他东西的结果。对于matplotlib 来说相对较新,我不确定空格来自哪里,即它是图形、轴还是plt,所以在谷歌上搜索没有提供任何相关答案。
【问题讨论】:
-
你指的是哪个空格?在六边形和黑色边框之间,还是在黑色边框之外?
标签: python matplotlib