pistachio
pistachio

Reputation: 3

How to add additional tick labels, formatted differently, in matplotlib

I'm having real problems plotting a 2nd line of tick labels on x-axis directly underneath the original ones. I'm using seaborn so I need to add these extra labels after the plot is rendered.

Below is what I'm trying to achieve but I'd like the gap between the 2 rows of tick labels to be a bit bigger and make the 2nd row bold and a different colour.

enter image description here

My attempts involve hacking the existing tick labels and appending the new strings underneath separated by newline:

            # n_labels is list that has the new labels I wish to plot directly
            # underneath the 1st row
            locs = ax.get_xticks().tolist()
            labels = [x.get_text() for x in ax.get_xticklabels()]
            nl = ['\n'.join([l, n_labels[i]]) for i, l in enumerate(labels)]
            ax.set_xticks(locs)
            ax.set_xticklabels(nl)

Any ideas? Thank you!

Upvotes: 0

Views: 720

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

One possibility would be to create a second x-axis on top of the first, and adjust the position and xtickslabels of the second x-axis. Check this example in the documentation as a starting point.

Upvotes: 1

Related Questions