简体   繁体   English

删除matplotlib中子图上的重叠刻度线

[英]remove overlapping tick marks on subplot in matplotlib

I've create the following set of subplots using the following function: 我使用以下函数创建了以下一组子图:

def create31fig(size,xlabel,ylabel,title=None):
    fig = plt.figure(figsize=(size,size))
    ax1 = fig.add_subplot(311)
    ax2 = fig.add_subplot(312)
    ax3 = fig.add_subplot(313)
    plt.subplots_adjust(hspace=0.001)
    plt.subplots_adjust(wspace=0.001)
    ax1.set_xticklabels([])
    ax2.set_xticklabels([])
    xticklabels = ax1.get_xticklabels()+ ax2.get_xticklabels()
    plt.setp(xticklabels, visible=False)
    ax1.set_title(title)
    ax2.set_ylabel(ylabel)
    ax3.set_xlabel(xlabel)
    return ax1,ax2,ax3

How do I make sure the top and bottom of subplot(312) do not overlap with their neighbours? 如何确保子图(312)的顶部和底部不与其邻居重叠? Thanks. 谢谢。

插曲

In the ticker module there is a class called MaxNLocator that can take a prune kwarg. 在自动收报机模块中有一个名为MaxNLocator的类,它可以采用修剪kwarg。
Using that you can remove the topmost tick of the 2nd and 3rd subplots: 使用它你可以删除第二和第三个子图的最顶部的刻度:

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator # added 

def create31fig(size,xlabel,ylabel,title=None):
    fig = plt.figure(figsize=(size,size))
    ax1 = fig.add_subplot(311)
    ax2 = fig.add_subplot(312)
    ax3 = fig.add_subplot(313)
    plt.subplots_adjust(hspace=0.001)
    plt.subplots_adjust(wspace=0.001)
    ax1.set_xticklabels([])
    ax2.set_xticklabels([])
    xticklabels = ax1.get_xticklabels() + ax2.get_xticklabels()
    plt.setp(xticklabels, visible=False)
    ax1.set_title(title)
    nbins = len(ax1.get_xticklabels()) # added 
    ax2.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) # added 
    ax2.set_ylabel(ylabel)
    ax3.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) # added 
    ax3.set_xlabel(xlabel)
    return ax1,ax2,ax3

create31fig(5,'xlabel','ylabel',title='test')

Sample image after making those adjustments: 进行这些调整后的示例图像:

在此输入图像描述

Aside: If the overlapping x- and y- labels in the lowest subplot are an issue consider "pruning" one of those as well. 旁白:如果最低子图中的重叠x和y标签是一个问题,也可以考虑“修剪”其中一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM