Adding Locator Tick Marks

When working with time series, it's often quite difficult to get the tick marks on charts looking the way you want to. This is why we have Locator helpers.

Using Locators we can change our x-axis from looking like this:

to looking like this:

The first step is importing matplotlib.dates.  This is where all the date plotting capabilities live.

Next, we need a YearLocator() and a MonthLocator() objects, which will help Matplotlib find the years and the months. Then we also need a DateFormatter(), which will help us specify how we want to display the dates. 

Now we can go back to our chart and format where the major and minor ticks should be using the Locators.

# format the ticksax1.xaxis.set_major_locator(years)ax1.xaxis.set_major_formatter(years_fmt)ax1.xaxis.set_minor_locator(months)

The final outcome should now look like this:

When we take a look at our chart, we can see the tick marks nicely. The tick marks also allow us to visually date that spike of interest in the middle of the chart - March 2016. This was when the Tesla Model 3 was unveiled. Also, we can clearly see that the most recent spikes in search coincide, not with the release of a new car, but the roaring stock price for the company!