关闭轴,保持刻度 [英] turn off axis, keep ticks

查看:36
本文介绍了关闭轴,保持刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我现在想摆脱围绕我的图的丑陋轴,但保持是指距离的刻度线.
我发现并尝试过的一切,例如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') 等关闭轴刻度.

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.

推荐答案

您可能决定不使用seaborn并将轴刺变为不可见:

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

使用rcParams可以完成相同的操作,

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

或者,如果您想使用seaborn(白色),还可以使用

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

正如@mwaskom 在评论中指出的那样,Seaborn 还提供了 sns.despine() 来去除刺,然后您可以像这样调用它

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)

注意双重否定(despine 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()

这篇关于关闭轴,保持刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆