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

后端 未结 11 2069
猫巷女王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 09:58
    df = pd.DataFrame({
    'client_scripting_ms' : client_scripting_ms,
     'apimlayer' : apimlayer, 'server' : server
    }, index = index)
    
    ax = df.plot(kind = 'barh', 
         stacked = True,
         title = "Chart",
         width = 0.20, 
         align='center', 
         figsize=(7,5))
    
    plt.legend(loc='upper right', frameon=True)
    
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('right')
    
    0 讨论(0)
  • 2020-11-27 10:00

    Building up on @peeol's excellent answer, you can also remove the frame by doing

    for spine in plt.gca().spines.values():
        spine.set_visible(False)
    

    To give an example (the entire code sample can be found at the end of this post), let's say you have a bar plot like this,

    you can remove the frame with the commands above and then either keep the x- and ytick labels (plot not shown) or remove them as well doing

    plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
    

    In this case, one can then label the bars directly; the final plot could look like this (code can be found below):

    Here is the entire code that is necessary to generate the plots:

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.figure()
    
    xvals = list('ABCDE')
    yvals = np.array(range(1, 6))
    
    position = np.arange(len(xvals))
    
    mybars = plt.bar(position, yvals, align='center', linewidth=0)
    plt.xticks(position, xvals)
    
    plt.title('My great data')
    # plt.show()
    
    # get rid of the frame
    for spine in plt.gca().spines.values():
        spine.set_visible(False)
    
    # plt.show()
    # remove all the ticks and directly label each bar with respective value
    plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
    
    # plt.show()
    
    # direct label each bar with Y axis values
    for bari in mybars:
        height = bari.get_height()
        plt.gca().text(bari.get_x() + bari.get_width()/2, bari.get_height()-0.2, str(int(height)),
                     ha='center', color='white', fontsize=15)
    plt.show()
    
    0 讨论(0)
  • 2020-11-27 10:02

    As I answered here, you can remove spines from all your plots through style settings (style sheet or rcParams):

    import matplotlib as mpl
    
    mpl.rcParams['axes.spines.left'] = False
    mpl.rcParams['axes.spines.right'] = False
    mpl.rcParams['axes.spines.top'] = False
    mpl.rcParams['axes.spines.bottom'] = False
    
    0 讨论(0)
  • 2020-11-27 10:03
    plt.box(False)
    plt.xticks([])
    plt.yticks([])
    plt.savefig('fig.png')
    

    should do the trick.

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

    To remove the frame of the chart

    for spine in plt.gca().spines.values():
      spine.set_visible(False)
    

    I hope this could work

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

    The easiest way to get rid of the the ugly frame in newer versions of matplotlib:

    import matplotlib.pyplot as plt
    plt.box(False)
    

    If you really must always use the object oriented approach, then do: ax.set_frame_on(False).

    0 讨论(0)
提交回复
热议问题