-
Book Overview & Buying
-
Table Of Contents
Matplotlib for Python Developers
We have seen X and Y ticks in every plot but we haven't yet noticed their presence explicitly.
Vertical and horizontal ticks are those little segments on the axes, usually coupled with axes labels, used to give a reference system on the graph (they are, for example, the origin of the grid lines).
Matplotlib provides two basic functions to manage them—xticks() and yticks(). They behave in the same way, so the description for one function will apply to the other too.
Executing with no arguments, the tick function returns the current ticks' locations and the labels corresponding to each of them:
locs, labels = plt.xticks()
The arguments (in the form of lists) that we can pass to the function are:
Locations of the ticks
Labels to draw at these locations (if necessary)
Let's try to explain it with an example:
In [1]: import matplotlib.pyplot as plt In [2]: x = [5, 3, 7, 2, 4, 1] In [3]: plt.plot(x); In [4]: plt.xticks(range(len(x)), ['a', 'b', 'c', 'd', 'e', 'f']); In [5]: plt...