Turn axes off for all subplots of a figure

后端 未结 1 564
旧时难觅i
旧时难觅i 2021-01-06 04:08

I am creating a large array of subplots and I want to turn off axes for all the subplots. Currently I am achieving this by

fig, ax = plt.subplots(7, len(clus         


        
相关标签:
1条回答
  • 2021-01-06 04:23

    I agree with @tcaswell that you should probably just use what you're already using. Another option to use it as a function is to use numpy.vectorize():

    import matplotlib.pyplot as plt
    import numpy as np
    fig, ax = plt.subplots(7, len(clusters))
    np.vectorize(lambda ax:ax.axis('off'))(ax)
    

    or, if you need to invoke it multiple times, by assigning the vectorized function to a variable:

    axoff_fun = np.vectorize(lambda ax:ax.axis('off'))
    # ... stuff here ...
    fig, ax = plt.subplots(7, len(clusters))
    axoff_fun(ax)
    

    Again, note that this is the same thing that @tcaswell suggested, in a fancier setting (only slower, probably). And it's essentially the same thing you're using now.


    However, if you insist on doing it some other way (i.e. you are a special kind of lazy), you can set matplotlib.rcParams once, and then every subsequent axes will automatically be off. There's probably an easier way to emulate axis('off'), but here's how I've succeeded:

    import matplotlib as mpl
    
    # before
    mpl.pyplot.figure()
    mpl.pyplot.plot([1,3,5],[4,6,5])
    
    # kill axis in rcParams
    mpl.rc('axes.spines',top=False,bottom=False,left=False,right=False);
    mpl.rc('axes',facecolor=(1,1,1,0),edgecolor=(1,1,1,0));
    mpl.rc(('xtick','ytick'),color=(1,1,1,0));
    
    # after
    mpl.pyplot.figure()
    mpl.pyplot.plot([1,3,5],[4,6,5])
    

    Result before/after:

    Hopefully there aren't any surprises which I forgot to override, but that would become clear quite quickly in an actual application anyway.

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