Option FanaticOptions, stock, futures, and system trading, backtesting, money management, and much more!

Resolving the X-Axis (Part 2)

I left off last time with a promising solution for setting x-axis labels using the Matplotlib.Ticker.FixedLocator Class. Unfortunately, the example at the bottom shows this doesn’t work for all values, which calls the solution into question.

What’s going on? Take a look at the following code snippet:

Zip code snippet 10 (6-9-22)

This shows for equally-spaced tick labels having integer coordinates, only certain numbers of labels are possible: 2, 3, 4, 5, 7, 10, and 20. I did not get six because it’s not mathematically possible. The same holds true for 8-9 and 11-19. When multiple equally-spaced lists are possible, I was really aiming for the one with the last element closest to the final date in the list.

In order to code this stuff accurately, I need to articulate exactly what I’m trying to achieve. I failed to do that.

Aside from the FixedLocator Class, another way to approach this is with np.linspace(a, b, c). This automatically creates a linear space of c-point subdivisions between a and b inclusive (i.e. a and b always included as the first and last values):

numpy linspace example (6-9-22)

Note how each list begins and ends with 0 (a) and 19 (b), respectively.

How do the plots look with different numbers of x-axis labels?

Graphing subplots with different number of x-axis labels by loop (6-9-22)

In the interest of space, I will describe rather than show the output. We get 20 subplots where the number of tick labels increases from zero to 19 by an increment of one for each subplot. The graphs are identical—the only thing that changes is the number of equally-spaced tick labels. Outstanding!

Some highlights of this code are as follows:

I’m quite happy with the progress made here!