Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

place labels between ticks

in matplotlib, how do i place ticks labels between ticks (not below ticks)

for example: when plotting a the stock price over time i would like the x axis minor ticks to display months and the years to show up between consecutive x axis major ticks (not just below the major ticks)

---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
  jan feb mar apr may jun jul aug sep oct nov dec jan feb mar apr may jun jul aug sep
                       2008                                            2009
like image 926
random guy Avatar asked Oct 25 '09 13:10

random guy


People also ask

How do you increase the space between ticks?

Create a figure and add a set of subplots. To set the ticks on a fixed position, create two lists with some values. Use set_yticks and set_xticks methods to set the ticks on the axes.

What is Xticks and Yticks in Python?

The xticks() and yticks() function takes a list object as argument. The elements in the list denote the positions on corresponding action where ticks will be displayed. ax. set_xticks([2,4,6,8,10]) This method will mark the data points at the given positions with ticks.

How do I center a tick label in MatPlotLib?

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.


2 Answers

Will this do the trick?

enter code here
x = 'j f m a m j j a s o n d j f m a m j j a s o n d'.split()
y = abs(randn(24))
x[6] = 'j\n2008' # replace "j" (January) with ('j' and the appropriate year
x[18] = 'j\n2009'
bar(xrange(len(x)), y, width=0.1)
bar(xrange(len(x)), y, width=0.1)
xticks(xrange(len(x)), x, ha='center')

Barchart with proper labels

like image 182
Boris Gorelik Avatar answered Oct 10 '22 20:10

Boris Gorelik


Do you mean something like this: - http://matplotlib.sourceforge.net/examples/pylab_examples/barchart_demo.html ??

You must use xticks and the ha (or 'horizontalalignment') parameter:

>>> x = 'j f m a m j j a s o n d'.split()
>>> y = abs(randn(12))
>>> bar(xrange(len(x)), y, width=0.1)

>>> xticks(xrange(len(x)), x, ha='center')

look at help(xticks) and help(matplotlib.text.Text) for more options

edit: sorry, I didn't see you are also asking for how to put the years labels below ticks. I think you must do it manually, have a look at the example I have linked to see how to do it.

like image 41
dalloliogm Avatar answered Oct 10 '22 20:10

dalloliogm