Matplotlib WeekdayLocator giving wrong dates/too many ticks
<p>I've been working with matplotlib.pyplot to plot some data over date ranges, but have been running across some weird behavior, not too different from <a href="https://stackoverflow.com/q/15165065/4739755">this question</a>.</p>

<p>The primary difference between my issue and that one (aside from the suggested fix not working) is they refer to different locators (WeekdayLocator() in my case, AutoDateLocator() in theirs.) As some background, here's what I'm getting:</p>

<p>The expected and typical result, where my data is displayed with a reasonable date range:
<a href="https://i.stack.imgur.com/NOiK1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NOiK1.png" alt="Good Results"></a></p>

<p>And the very occasional result, where the data is given some ridiculous range of about 5 years (from what I can see):
<a href="https://i.stack.imgur.com/8B1FH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8B1FH.png" alt="Bad Results"></a></p>

<p>I did some additional testing with a generic matplotlib.pyplot.plot and it seemed to be unrelated to using a subplot, or just creating the plot using the module directly.</p>

<pre><code>plt.plot(some plot)
</code></pre>

<p>vs.</p>

<pre><code>fig = plt.figure(...)
sub = fig.add_subplot(...)
sub.plot(some plot)
</code></pre>

<p>From what I could find, the odd behavior only happens when the data set only has one point (and therefore only having a single date to plot). The outrageous number of ticks is caused by the WeekdayLocator() which, for some reason, attempts to generate 1653 ticks for the x-axis date range (from about 2013 to 2018) based on this error output:</p>

<pre><code>RuntimeError: RRuleLocator estimated to generate 1635 ticks from
2013-07-11 19:23:39+00:00 to 2018-01-02 00:11:39+00:00: exceeds Locator.MAXTICKS * 2 (20)
</code></pre>

<p>(This was from some experimenting with the WeekdayLocator().MAXTICKS member set to 10)</p>

<p>I then tried changing the Locator based on how many date points I had to plot:</p>

<pre><code># If all the entries in the plot dictionary have &lt;= 1 data point to plot
if all(len(times[comp]) &lt;= 1 for comp in times.keys()):
    sub.xaxis.set_major_locator(md.DayLocator())
else:
    sub.xaxis.set_major_locator(md.WeekdayLocator())
</code></pre>

<p>This worked for the edge cases where I'd have a line 2 points and a line with 1 (or just a point) and wanted the normal ticking since it didn't get messed up, but only <em>sort of</em> worked to fix my problem:</p>

<p><a href="https://i.stack.imgur.com/AYi8U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AYi8U.png" alt="Almost..."></a></p>

<p>Now I don't have a silly amount of tick marks, but my date range is still 5 years! (Side Note: I also tried using an HourLocator(), but it attempted to generate almost 40,000 tick marks...)</p>

<p>So I guess my question is this: is there some way to rein in the date range explosion when only having one date to plot, or am I at the mercy of a strange bug with Matplotlib's date plotting methods?</p>

<p>What I would like to have is something similar to the first picture, where the date range goes from a little before the first date and a little after the last date. Even if Matplotlib were to fill up the axis range to about match the frequency of ticks in the first image, I would expect it to only span the course of a month or so, not five whole years.</p>

<p><strong>Edit:</strong></p>

<p>Forgot to mention that the range explosion also appears to occur regardless of which Locator I use. Plotting with zero points just results in a blank x-axis (due to no date range at all), a single point gives me the described huge date range, and multiple points/lines gives the expected date ranges.</p>