【问题标题】:Increase Axis Thickness in Matplotlib (Without Cutting into Plot Domain!)在 Matplotlib 中增加轴厚度(不切入绘图域!)
【发布时间】:2019-03-13 12:11:19
【问题描述】:

我对增加 matplotlib 中轴厚度的方法感兴趣(不切入绘图域)。也就是说,我希望轴的厚度从绘图向外延伸,而不是向内延伸。这样的事情可能吗?

传统方法(例如,见下文)似乎行不通:

from pylab import *

close("all")
#rc('axes', linewidth=30)

# Make a dummy plot
plot([0.01, 0, 1], [0.5, 0, 1])

fontsize = 14
ax = gca()

for axis in ['top','bottom','left','right']:
  ax.spines[axis].set_linewidth(30)

xlabel('X Axis', fontsize=16, fontweight='bold')
ylabel('Y Axis', fontsize=16, fontweight='bold')

【问题讨论】:

    标签: python matplotlib plot axis-labels yaxis


    【解决方案1】:

    具有相同效果的选项是创建一个与坐标轴范围完全相同的白色矩形,这样坐标轴内的脊椎部分就会被矩形隐藏。这将需要使线宽两倍大,因为只能看到一半的线。

    import matplotlib.pyplot as plt
    
    # Make a dummy plot
    fig, ax = plt.subplots()
    ax.plot([0.01, 0, 1], [0.5, 0, 1], zorder=1)
    
    ax.axis([0,1,0,1])
    
    
    for axis in ['top','bottom','left','right']:
        ax.spines[axis].set_linewidth(30)
        ax.spines[axis].set_color("gold")
        ax.spines[axis].set_zorder(0)
    
    ax.add_patch(plt.Rectangle((0,0),1,1, color="w", transform=ax.transAxes))
    
    
    ax.set_xlabel('X Axis', fontsize=16, fontweight='bold')
    ax.set_ylabel('Y Axis', fontsize=16, fontweight='bold')
    
    plt.show()
    

    我在这里把刺变成黄色,这样它们就不会隐藏刻度和刻度标签。

    另一种选择是调整Set matplotlib rectangle edge to outside of specified width? 的答案以创建一个矩形,该矩形严格包围图中的一个区域,如下所示:

    import matplotlib.pyplot as plt
    from matplotlib.patches import Rectangle
    from matplotlib.offsetbox import AnnotationBbox, AuxTransformBox
    
    # Make a dummy plot
    fig, ax = plt.subplots()
    ax.plot([0.01, 0, 1], [0.5, 0, 1], zorder=1)
    
    ax.axis([0,1,0,1])
    
    linewidth=14
    xy, w, h = (0, 0), 1, 1
    r = Rectangle(xy, w, h, fc='none', ec='k', lw=1, transform=ax.transAxes)
    
    offsetbox = AuxTransformBox(ax.transData)
    offsetbox.add_artist(r)
    ab = AnnotationBbox(offsetbox, (xy[0]+w/2.,xy[1]+w/2.),
                        boxcoords="data", pad=0.52,fontsize=linewidth,
                        bboxprops=dict(facecolor = "none", edgecolor='r', 
                                  lw = linewidth))
    ab.set_zorder(0)
    ax.add_artist(ab)
    
    ax.set_xlabel('X Axis', fontsize=16, fontweight='bold')
    ax.set_ylabel('Y Axis', fontsize=16, fontweight='bold')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      相关资源
      最近更新 更多