Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Preventing xticks from overlapping yticks

How can I prevent the labels of xticks from overlapping with the labels of yticks when using hist (or other plotting commands) in matplotlib?

                                                          enter image description here

Upvotes: 5

Views: 614

Answers (2)

EnricoGiampieri
EnricoGiampieri

Reputation: 6085

There are several ways.

One is to use the tight_layout method of the figure you are drawing, which will automatically try to optimize the appareance of the labels.

fig, ax = subplots(1)
ax.plot(arange(10),rand(10))
fig.tight_layout()

An other way is to modify the rcParams values for the ticks formatting:

 rcParams['xtick.major.pad'] = 6

This will draw the ticks a little farter from the axes. after modifying the rcparams (this of any other, you can find the complete list on your matplotlibrc configuration file), remember to set it back to deafult with the rcdefaults function.

A third way is to tamper with the axes locator_params telling it to not draw the label in the corner:

fig, ax = subplots(1)
ax.plot(arange(10),rand(10))
ax.locator_params(prune='lower',axis='both')

the axis keywords tell the locator on which axis it should work and the prune keyword tell it to remove the lowest value of the tick

Upvotes: 2

Paul H
Paul H

Reputation: 68116

Try increasing the padding between the ticks on the labels

import matplotlib
matplotlib.rcParams['xtick.major.pad'] = 8 # defaults are 4
matplotlib.rcParams['ytick.major.pad'] = 8 

same goes for [x|y]tick.minor.pad.

Also, try setting: [x|y]tick.direction to 'out'. That gives you a little more room and helps makes the ticks a little more visible -- especially on histograms with dark bars.

Upvotes: 2

Related Questions