关闭上/右轴刻度线

时间:2012-07-10 09:56:21

标签: python matplotlib

我想让右轴和上轴上的刻度不可见,并且不确定第三行应该是什么:

import matplotlib.pyplot as plt
plt.plot(X,Y)
#plt.upper_right_axis_ticks_off()

4 个答案:

答案 0 :(得分:16)

正如@imsc指出的那样,你可以通过使用ax.xaxis.set_ticks_position将刻度线的位置设置在左下角(如果你不希望它们在顶部和右边)来调整刻度线的可见性。和ax.yaxis.set_ticks_position方法。

如果您还想将轴本身设置为不可见,请查看matplotlib.spines类。您只需使用none方法将刺的颜色设置为ax.spines[spine].set_color

如果您要显示很多图表,并且不希望每次都手动关闭轴/标记,则下面是一个可以为您完成工作的功能。

基本上,您为每个轴定义所需的颜色(在这种情况下none将使其不可见),并且该功能还将“关闭”所有轴不可见的刻度标记。我还添加了选项来定义轴线的linewidth,以及勾选标签的fontsize和勾选标签与刻度线之间的pad

def customaxis(ax, c_left='k', c_bottom='k', c_right='none', c_top='none',
               lw=3, size=12, pad=8):

    for c_spine, spine in zip([c_left, c_bottom, c_right, c_top],
                              ['left', 'bottom', 'right', 'top']):
        if c_spine != 'none':
            ax.spines[spine].set_color(c_spine)
            ax.spines[spine].set_linewidth(lw)
        else:
            ax.spines[spine].set_color('none')
    if (c_bottom == 'none') & (c_top == 'none'): # no bottom and no top
        ax.xaxis.set_ticks_position('none')
    elif (c_bottom != 'none') & (c_top != 'none'): # bottom and top
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                      color=c_bottom, labelsize=size, pad=pad)
    elif (c_bottom != 'none') & (c_top == 'none'): # bottom but not top
        ax.xaxis.set_ticks_position('bottom')
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                       color=c_bottom, labelsize=size, pad=pad)
    elif (c_bottom == 'none') & (c_top != 'none'): # no bottom but top
        ax.xaxis.set_ticks_position('top')
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                       color=c_top, labelsize=size, pad=pad)
    if (c_left == 'none') & (c_right == 'none'): # no left and no right
        ax.yaxis.set_ticks_position('none')
    elif (c_left != 'none') & (c_right != 'none'): # left and right
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_left, labelsize=size, pad=pad)
    elif (c_left != 'none') & (c_right == 'none'): # left but not right
        ax.yaxis.set_ticks_position('left')
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_left, labelsize=size, pad=pad)
    elif (c_left == 'none') & (c_right != 'none'): # no left but right
        ax.yaxis.set_ticks_position('right')
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_right, labelsize=size, pad=pad)

以下是如何使用它的示例:

import numpy as np
import matplotlib.pyplot as plt

fig, ([ax1, ax2], [ax3, ax4]) = plt.subplots(nrows=2, ncols=2)

for ax in [ax1, ax2, ax3, ax4]:
    ax.plot(np.random.randn(10), lw=2)

customaxis(ax1) #default: no right and top axis
customaxis(ax2, c_left='none', c_bottom='none', c_right='k', c_top='k')
customaxis(ax3, c_left='none', c_bottom='k', c_right='k', c_top='none')
customaxis(ax4, c_left='k', c_bottom='none', c_right='none', c_top='k')

plt.show()

spines

答案 1 :(得分:11)

查看http://matplotlib.sourceforge.net/examples/pylab_examples/spine_placement_demo.html

import pylab as p
t = p.arange(0.0, 2.0, 0.01)
ax=p.subplot(111)
s = p.sin(2*p.pi*t)
ax.plot(t, s, color='r',linewidth=1.0)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
p.show()

答案 2 :(得分:9)

tick_params应该这样做。

我当前的计算机上没有安装matplotlib,所以我无法测试它,但这应该可行:

import matplotlib.pyplot as plt

ax = plt.subplot(111)
plt.plot([0,1], [0,1])
ax.tick_params(labeltop='off', labelright='off')
plt.show()

答案 3 :(得分:3)

plt.tick_params(top='off', right='off', which='both') 

我在上面的代码中使用了'on'而不是'off',用于在matplotlib 2.0中显示顶部和右侧的刻度线以及默认的“bottom”和“left”