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?
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')
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.
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.
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.
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:
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)
Before:
After:
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()])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With