turn off axis, keep ticks

后端 未结 1 553
南旧
南旧 2021-01-22 20:57

I am plotting an image using plt.imshow() using the seaborn add on....

from astropy.io import fits
import numpy as np
import matplotlib.pyplot as plt
import seab         


        
相关标签:
1条回答
  • 2021-01-22 21:57

    You may decide not to use seaborn and turn the axes spines invisible:

    for d in ["left", "top", "bottom", "right"]:
        plt.gca().spines[d].set_visible(False)
    

    import numpy as np
    import matplotlib.pyplot as plt
    
    mapy = np.random.rand(100,100)
    pf = 2.8
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    
    fig = plt.imshow(mapy,interpolation='spline16',origin='lower',
                     cmap='Reds',extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    
    for d in ["left", "top", "bottom", "right"]:
        plt.gca().spines[d].set_visible(False)
    
    plt.show()

    The same can be done, using rcParams,

    s = {"axes.spines.left"   : False,
        "axes.spines.bottom" : False,
        "axes.spines.top"    : False,
        "axes.spines.right"  : False}
    plt.rcParams.update(s)
    

    import numpy as np
    import matplotlib.pyplot as plt
    s = {"axes.spines.left"   : False,
    "axes.spines.bottom" : False,
    "axes.spines.top"    : False,
    "axes.spines.right"  : False}
    plt.rcParams.update(s)
    
    mapy = np.random.rand(100,100)
    pf = 2.8
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    
    fig = plt.imshow(mapy,interpolation='spline16',origin='lower',
                 cmap='Reds',extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    
    plt.show()

    Alternatively you can set the axes edgecolor to transparent.

    plt.rcParams["axes.edgecolor"]=(1,1,1,0)
    

    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams["axes.edgecolor"]=(1,1,1,0)
    
    mapy = np.random.rand(100,100)
    pf = 2.8
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    
    fig = plt.imshow(mapy,interpolation='spline16',origin='lower',
                 cmap='Reds',extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    
    plt.show()

    Or, if you want to use seaborn (and it's white-style), additionally reactivate the ticks using

    plt.tick_params(axis="both", which="major", length=5)
    

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    sns.set_style(style='white') #turn off grid
    
    mapy = np.random.rand(100,100)
    pf = 2.8
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    
    fig = plt.imshow(mapy,interpolation='spline16',origin='lower',
                     cmap='Reds',extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    
    for d in ["left", "top", "bottom", "right"]:
        plt.gca().spines[d].set_visible(False)
    
    plt.tick_params(axis="both", which="major", length=5)
    plt.show()

    As @mwaskom points out in the comments, Seaborn also offers sns.despine() to get rid of the spines, which you would then call like

    sns.despine(left=True, top=True, bottom=True, right=True)
    

    Note the double negation (despine True means not to have spines).

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    sns.set_style(style='white') #turn off grid
    
    mapy = np.random.rand(100,100)
    pf = 2.8
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    
    fig = plt.imshow(mapy,interpolation='spline16',origin='lower',
                 cmap='Reds',extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    sns.despine(left=True, top=True, bottom=True, right=True)
    
    plt.tick_params(axis="both", which="major", length=5)
    plt.show()

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