关闭上/右轴刻度线 [英] Turn off the upper/right axis tick marks

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

问题描述

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

I want to make the ticks on the right and upper axis invisible and am not sure what the third line should be:

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

推荐答案

如@imsc所指出的,您可以通过将刻度线的位置设置在底部和左侧来调整刻度线的可见性(如果不这样做)希望它们在顶部和右侧)使用ax.xaxis.set_ticks_positionax.yaxis.set_ticks_position方法.

As pointed by @imsc, You can tweak the visibility of the tick marks by setting the position of the ticks to the bottom and left (if you don't want them on top and right) using the ax.xaxis.set_ticks_position and ax.yaxis.set_ticks_position methods.

如果您还想将轴本身设置为不可见,请查看

If you also want to set the axis itself invisible, check out the matplotlib.spines class. You just have to set the color of the spines to none using the ax.spines[spine].set_color method.

如果要显示很多图,并且不想每次都手动关闭轴/刻度,则下面的功能将为您完成这项工作.

If you have a lot of plots to display, and don't want to turn manually the axis/ticks off each time, below is a function that will do the job for you.

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

Basically, you define for each axis the color you want (in this case none will render it invisible), and the function will also "turn off" the ticks marks for all the axis invisible. I have also added options to define the linewidth of the axis lines, as well as the fontsize of the ticklabels and the pad between the ticklabels and the ticks.

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)

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

Below is an example of how to use it:

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

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

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