关闭轴,保持刻度

Seb*_*991 2 matplotlib seaborn

我正在使用 plt.imshow() 使用 seaborn 添加绘制图像....

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

sns.set_style(style='white') #turn off grid

def plotter():
    mapy = np.zeros((100,100))
    pf = 2.8 #my physical pixel size
    areaX = mapy.shape[0]/2*pf # half of the area!
    areaY = mapy.shape[1]/2*pf # half of the area!
    cmapz ='Reds'
    fig = imshow(mapy,interpolation='spline16',origin='lower',cmap=cmapz,extent=[-areaX*pf,areaX*pf,-areaY*pf,areaY*pf])
    plt.grid(False)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

我现在想摆脱围绕我的情节的丑陋轴,但在它们指的是距离时将刻度保持在适当的位置。
一切,我发现并试图,例如plt.axis('off')fig.axes.get_yaxis().set_visible(False)plt.tick_params(top='off', bottom='on', left='off', right='off', labelleft='on', labelbottom='on') 等关闭两个轴刻度线。

Imp*_*est 5

您可以决定不使用 seaborn 并将轴脊转为隐形:

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

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

在此处输入图片说明

使用 rcParams 也可以这样做,

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

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()
Run Code Online (Sandbox Code Playgroud)

或者,您可以将轴边缘颜色设置为透明。

plt.rcParams["axes.edgecolor"]=(1,1,1,0)
Run Code Online (Sandbox Code Playgroud)

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

或者,如果您想使用 seaborn(并且是白色风格),还可以使用

plt.tick_params(axis="both", which="major", length=5)
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

正如@mwaskom 在评论中指出的那样,Seaborn 还提供sns.despine()去除刺的功能,然后您可以将其称为

sns.despine(left=True, top=True, bottom=True, right=True)
Run Code Online (Sandbox Code Playgroud)

请注意双重否定(de spin True 表示没有刺)。

plt.rcParams["axes.edgecolor"]=(1,1,1,0)
Run Code Online (Sandbox Code Playgroud)