【问题标题】:Matplotlib remove white space around patches [duplicate]Matplotlib删除补丁周围的空白[重复]
【发布时间】: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


【解决方案1】:

II 建议阅读 x 和 y 限制以了解如何根据需要调整它们,即:

print(ax.get_xlim())

然后例如

ax.set_xlim(0.5, 5.5)

或任何合适的。

与y轴相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 2017-05-28
    • 2017-02-25
    • 2021-06-19
    • 2016-02-25
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多