Osmond Osmond - 1 year ago 221
Python Question

Matplotlib - hiding specific ticks on x-axis

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?

Answer Source

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)
    plt.ylabel ('Some label')

    make_invisible = True
    if (make_invisible):
        xticks = ax.xaxis.get_major_ticks()


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download