问题
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 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()
I now want to get rid off the ugly axis around my plot, but KEEP the ticks in place as they refer to the distance.
Everything I found and tried, e.g. 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') etc. turns off both the axis AND the ticks.
回答1:
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()
来源:https://stackoverflow.com/questions/44525032/turn-off-axis-keep-ticks