关闭轴,保持刻度

turn off axis, keep ticks

我正在使用 seaborn 插件使用 plt.imshow() 绘制图像....

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()

我现在想摆脱围绕我的地块的丑陋轴,但保持刻度线在适当的位置,因为它们指的是距离。
我发现并尝试过的一切,例如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') 等关闭轴 刻度。

您可以决定不使用 seaborn 并将轴刺隐藏:

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()

同样可以做到,使用 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()

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

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()

或者,如果你想使用 seaborn(它是白色风格的),另外使用

重新激活刻度线
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()

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

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

注意双重否定(despine True 表示没有spine)

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()