Search code examples
pythonmatplotlibaxis-labels

How to show xaxis ticklabels in a grid


How to show xaxis ticklabels below the grid?

ax.set_xticklabels([0.1, '', '', '', 0.5, '', '', '', '', 1.0], minor=True)

When I run the code I get the figure below.

enter image description here

If I change the line of code from

ax.set_xticks(np.arange(0, len(diameter_labels), 1))

to

ax.set_xticks(np.arange(0, len(diameter_labels), 1), minor=True)

This time I got the following figure.

enter image description here

In fact, I want to get the following figure.

enter image description here

import matplotlib.pyplot as plt
import os
import numpy as np

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 3))
    diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

    row_labels = ['circle']
    ax.grid(which="major", color="white", linestyle='-', linewidth=3)

    ax.set_aspect(1.0)

    for row_index, row_label in enumerate(row_labels):
        for diameter_index, diameter_label in enumerate(diameter_labels):
            circle=plt.Circle((diameter_index + 0.5, row_index + 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
            ax.add_artist(circle)

    ax.set_xlim([0, len(diameter_labels)])
    ax.set_xticklabels([])

    ax.set_xticks(np.arange(0, len(diameter_labels), 1))
    ax.tick_params(axis='x', which='minor', length=0, labelsize=30)
    ax.set_xticklabels([0.1, '', '', '', 0.5, '', '', '', '', 1.0], minor=True)
    ax.xaxis.set_ticks_position('bottom')

    ax.tick_params(
        axis='x',  # changes apply to the x-axis
        which='major',  # both major and minor ticks are affected
        bottom=False,  # ticks along the bottom edge are off
        top=False)  # labels along the bottom edge are off

    ax.xaxis.set_label_position('top')
    ax.set_xlabel('Test', fontsize=40, labelpad=5)

    ax.set_ylim([0, len(row_labels)])
    ax.set_yticklabels([])

    ax.tick_params(axis='y', which='minor', length=0, labelsize=12)
    ax.set_yticks(np.arange(0, len(row_labels), 1))

    ax.tick_params(
        axis='y',
        which='major',
        left=False)

    ax.grid(which='major', color='black', linestyle='-', linewidth=1)

    filename = 'test.png'
    figureFile = os.path.join('/Users/burcakotlu/Desktop', filename)
    fig.savefig(figureFile)
    plt.close()

Solution

  • Replacing:

    ax.set_xticks(np.arange(0, len(diameter_labels), 1), minor=True)
    

    with:

    ticks = np.arange(0, len(diameter_labels) + 1)
    labels = ticks.astype(str)
    ax.set_xticks(ticks, labels)
    

    I get this. Read help(ax.set_xticks) for more information.

    enter image description here

    EDIT to satisfy comment:

    Then you modify the label list according to what you would like to achieve, something like this:

    ticks = np.arange(0, len(diameter_labels) + 1)
    labels = [0.1, '', '', '', 0.5, '', '', '', '', 1.0, ""]
    ax.set_xticks(ticks, labels)
    

    enter image description here

    EDIT third time is the charm:

    fig, ax = plt.subplots(figsize=(10, 3))
    diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    
    row_labels = ['circle']
    ax.grid(which="major", color="white", linestyle='-', linewidth=3)
    
    ax.set_aspect(1.0)
    
    for row_index, row_label in enumerate(row_labels):
        for diameter_index, diameter_label in enumerate(diameter_labels):
            circle=plt.Circle((diameter_index + 0.5, row_index + 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
            ax.add_artist(circle)
    
    ax.set_xlim([0, len(diameter_labels)])
    ax.set_ylim([0, len(row_labels)])
    
    ax.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom=False,  # ticks along the bottom edge are off
        top=False)  # labels along the bottom edge are off
    
    ax.grid(which='major', color='black', linestyle='-', linewidth=1)
    yticks = np.arange(0, len(row_labels))
    ytickslabels = [] * len(yticks)
    ax.set_yticks(yticks, ytickslabels)
    
    xticks = np.arange(0, len(diameter_labels) + 1)
    ax.set_xticks(xticks, xticks)
    
    from matplotlib import ticker
    ax.xaxis.set_major_formatter(ticker.NullFormatter())   # remove the major ticks
    ax.yaxis.set_major_formatter(ticker.NullFormatter())   # remove the major ticks
    
    ax.minorticks_on()
    xminorticks = [0.5, 4.5, 9.5]
    xminortickslabels = ["0.1", "0.5", "1"]
    ax.set_xticks(xminorticks, xminortickslabels, minor=True)
    

    enter image description here