Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering x-tick labels between tick marks in matplotlib

Tags:

I want to have the x-tick date labels centered between the tick marks, instead of centered about the tick marks as shown in the photo below.

I have read the documentation but to no avail - does anyone know a way to do this?

enter image description here

Here is everything that I've used for my x-axis tick formatting if it helps:

day_fmt = '%d'    myFmt = mdates.DateFormatter(day_fmt) ax.xaxis.set_major_formatter(myFmt)     ax.xaxis.set_major_locator(matplotlib.dates.DayLocator(interval=1))       for tick in ax.xaxis.get_major_ticks():     tick.tick1line.set_markersize(0)     tick.tick2line.set_markersize(0)     tick.label1.set_horizontalalignment('center') 
like image 210
Osmond Bishop Avatar asked Jun 17 '13 23:06

Osmond Bishop


People also ask

How do you center X ticks?

However there is no direct way to center the labels between ticks. To fake this behavior, one can place a label on the minor ticks in between the major ticks, and hide the major tick labels and minor ticks. Here is an example that labels the months, centered between the ticks.

How do I increase the space between X-axis ticks?

The spacing between ticklabels is exclusively determined by the space between ticks on the axes. Therefore the only way to obtain more space between given ticklabels is to make the axes larger.

How do you space out the X-axis labels in python?

MatPlotLib with Python To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.


2 Answers

One way to do it is to use the minor ticks. The idea is that you set the minor ticks so that they are located halfway between the major ticks, and you manually specify the labels.

For example:

import matplotlib.ticker as ticker  # a is an axes object, e.g. from figure.get_axes()  # Hide major tick labels a.xaxis.set_major_formatter(ticker.NullFormatter())  # Customize minor tick labels a.xaxis.set_minor_locator(ticker.FixedLocator([1.5,2.5,3.5,4.5,5.5])) a.xaxis.set_minor_formatter(ticker.FixedFormatter(['1','2','3','4','5'])) 

The three lines:

  • "Hide" the 1,2,3,4,... that you have on the major ticks
  • Set minor ticks halfway between the major ticks (assuming your major ticks are at 1,2,3...)
  • Manually specifies the labels for the minor ticks. Here, '1' would be between 1.0 and 2.0 on the graph.

This is just a simple example. You would probably want to streamline it a bit by populating the lists in a loop or something.

You can also experiment with other locators or formatters.

Edit: Alternatively, as suggested in the comments:

# Hide major tick labels a.set_xticklabels('')  # Customize minor tick labels a.set_xticks([1.5,2.5,3.5,4.5,5.5],      minor=True) a.set_xticklabels(['1','2','3','4','5'], minor=True) 

Example:

Before: Before

After: enter image description here

like image 111
jedwards Avatar answered Sep 22 '22 15:09

jedwards


Here's an alternative to using Locators and Formatters. It can be used for any spacings between labels:

# tick_limit: the last tick position without centering (16 in your example) # offset: how many steps between each tick (1 in your example) # myticklabels: string labels, optional (range(1,16) in your example)  # need to set limits so the following works: ax.xaxis.set_ticks([0, tick_limit])  # offset all ticks between limits: ax.xaxis.set(ticks=np.arange(offset/2., tick_limit, offset), ticklabels=myticklabels) # turn off grid ax.grid(False) 

Since this modifies the major ticks, the grid might have to be adjusted - depending on the application. It's also possible to work around this by using ax.twinx()). This will result in moving the labels on the opposite side of a separate axis but will leave the original grid untouched and giving two grids, one for the original ticks and one for the offsets.

Edit:

Assuming evenly spaced integer ticks, this is probably the most simple way:

ax.set_xticks([float(n)+0.5 for n in ax.get_xticks()]) 
like image 39
runDOSrun Avatar answered Sep 20 '22 15:09

runDOSrun