Matplotlib-在X轴上隐藏特定的刻度 [英] Matplotlib - hiding specific ticks on x-axis

查看:1891
本文介绍了Matplotlib-在X轴上隐藏特定的刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图隐藏我的条形图的第一个和最后一个x轴刻度文本,分别是"2004"和"2013"​​. Matplotlib默认情况下会自动添加这些内容,即使我的数据集是2005年至2012年,因此我也不希望在柱状图中不包含2004年和2013年.我正在寻找一些代码行来选择和隐藏这些刻度.有任何想法吗?

I am trying to hide the first and last x-axis tick text of my bar plot, which is '2004' and '2013'. Matplotlib automatically adds these in by default, even though my dataset is for 2005 to 2012, hence I'd prefer not to have 2004 and 2013 in my bar plot. I'm looking for some lines of code to select and hide these ticks. Any ideas?

推荐答案

请告诉我它是否不是您想要的.

Please, tell me if it's not what you want.

import sys, os
import matplotlib.pyplot as plt

path = sys.path[0]
sizes = [(12,3,), (4,3,)]
x =  range(20)


for i, size in enumerate(sizes):
    fig = plt.figure(figsize = size, dpi = 80, facecolor='white',edgecolor=None,linewidth=0.0, frameon=True, subplotpars=None)
    ax = fig.add_subplot(111)
    ax.plot(x)
    plt.ylabel ('Some label')
    plt.tight_layout()

    make_invisible = True
    if (make_invisible):
        xticks = ax.xaxis.get_major_ticks()
        xticks[0].label1.set_visible(False)
        xticks[-1].label1.set_visible(False)

plt.show()

此示例使第一个和最后一个X标记不可见.但是您可以轻松添加对特殊刻度的检查.

This example makes invisible first and last X-ticks. But you can easily add checking for your special ticks.

这篇关于Matplotlib-在X轴上隐藏特定的刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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