<p dir="auto">This was posted onto stackoverflow but it seems more like a bug than anything:</p>
<p dir="auto"><a href="http://stackoverflow.com/questions/43242246/pandas-dataframe-groupby-failed-zooming-for-x-axis-ticks" rel="nofollow">http://stackoverflow.com/questions/43242246/pandas-dataframe-groupby-failed-zooming-for-x-axis-ticks</a></p>
<p dir="auto">As pointed out here (<a href="https://stackoverflow.com/questions/24468333/set-xlim-for-pandas-matplotlib-where-index-is-string/24477138#24477138" rel="nofollow">https://stackoverflow.com/questions/24468333/set-xlim-for-pandas-matplotlib-where-index-is-string/24477138#24477138</a>) if the index of a data frame is objects (that is not, the index obviously convertible to scalars and the data is being plotted against <code class="notranslate">range(len(df))</code>), the ticklabels are set to be string representations of the objects (I presume via <code class="notranslate">set_xticklabels</code>) but the locator is still a <code class="notranslate">AutoLocator</code> which means if you change the xlimits or pan/zoom the axes, the ticklabels become de-coupled from the data.</p>
<p dir="auto">A solution is to also use a fixed locator which will pin the ticks to only be on the index locations.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

df = pd.DataFrame({'Foo': pd.Series([2,3,4], index=['2002', '2003', '2004'])})
fig, ax = plt.subplots()

df.plot(ax=ax)

ax.xaxis.set_major_locator(mticker.FixedLocator(np.arange(len(df))))
ax.xaxis.set_major_formatter(mticker.FixedFormatter(df.index))"><pre class="notranslate"><code class="notranslate">import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

df = pd.DataFrame({'Foo': pd.Series([2,3,4], index=['2002', '2003', '2004'])})
fig, ax = plt.subplots()

df.plot(ax=ax)

ax.xaxis.set_major_locator(mticker.FixedLocator(np.arange(len(df))))
ax.xaxis.set_major_formatter(mticker.FixedFormatter(df.index))
</code></pre></div>
<p dir="auto">I can make changes to the code if someone points me to where the relevant section is.</p>