关掉轴,保持嘀嗒声

时间:2017-06-13 14:56:43

标签: matplotlib seaborn

我使用seaborn add on使用plt.imshow()绘制图像....

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'),{{1}}等会关闭轴 AND

1 个答案:

答案 0 :(得分:3)

您可能决定不使用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()

enter image description here

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

或者,您可以将轴edgecolor设置为透明。

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

enter image description here

正如@mwaskom在评论中指出的那样,Seaborn还提供sns.despine()来摆脱刺,你会称之为

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

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

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