How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)

后端 未结 11 2070
猫巷女王i
猫巷女王i 2020-11-27 09:53

To remove frame in figure, I write

frameon=False

works perfect with pyplot.figure, but with matplotlib.Figure it

相关标签:
11条回答
  • 2020-11-27 10:10
    plt.axis('off')
    plt.savefig(file_path, bbox_inches="tight", pad_inches = 0)
    

    plt.savefig has those options in itself, just need to set axes off before

    0 讨论(0)
  • 2020-11-27 10:15

    I use to do so:

    from pylab import *
    axes(frameon = 0)
    ...
    show()
    
    0 讨论(0)
  • 2020-11-27 10:16

    First off, if you're using savefig, be aware that it will override the figure's background color when saving unless you specify otherwise (e.g. fig.savefig('blah.png', transparent=True)).

    However, to remove the axes' and figure's background on-screen, you'll need to set both ax.patch and fig.patch to be invisible.

    E.g.

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    ax.plot(range(10))
    
    for item in [fig, ax]:
        item.patch.set_visible(False)
    
    with open('test.png', 'w') as outfile:
        fig.canvas.print_png(outfile)
    

    enter image description here

    (Of course, you can't tell the difference on SO's white background, but everything is transparent...)

    If you don't want to show anything other than the line, turn the axis off as well using ax.axis('off'):

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    ax.plot(range(10))
    
    fig.patch.set_visible(False)
    ax.axis('off')
    
    with open('test.png', 'w') as outfile:
        fig.canvas.print_png(outfile)
    

    enter image description here

    In that case, though, you may want to make the axes take up the full figure. If you manually specify the location of the axes, you can tell it to take up the full figure (alternately, you can use subplots_adjust, but this is simpler for the case of a single axes).

    import matplotlib.pyplot as plt
    
    fig = plt.figure(frameon=False)
    ax = fig.add_axes([0, 0, 1, 1])
    ax.axis('off')
    
    ax.plot(range(10))
    
    with open('test.png', 'w') as outfile:
        fig.canvas.print_png(outfile)
    

    enter image description here

    0 讨论(0)
  • 2020-11-27 10:24

    ax.axis('off'), will as Joe Kington pointed out, remove everything except the plotted line.

    For those wanting to only remove the frame (border), and keep labels, tickers etc, one can do that by accessing the spines object on the axis. Given an axis object ax, the following should remove borders on all four sides:

    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    

    And, in case of removing x and y ticks from the plot:

     ax.get_xaxis().set_ticks([])
     ax.get_yaxis().set_ticks([])
    
    0 讨论(0)
  • 2020-11-27 10:24

    Problem

    I had a similar problem using axes. The class parameter is frameon but the kwarg is frame_on. axes_api
    >>> plt.gca().set(frameon=False)
    AttributeError: Unknown property frameon

    Solution

    frame_on

    Example

    data = range(100)
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot(data)
    #ax.set(frameon=False)  # Old
    ax.set(frame_on=False)  # New
    plt.show()
    
    0 讨论(0)
提交回复
热议问题