Spanselector zoom in doesn't work with datetime in x-axis

Hi all

I have a dataframe with a datetime objects ndarray in a column and a float values in another column.
I’m trying to use spanselector to show datetime values in the x-axis and the corresponding float values in the y-axis.
What I obtain is:
In the upper graph, the whole data plot with the datetimes correctly, and when I press and drag with the mouse, the red selection in the area is shown.
However, in the bottom graph, the one to show the zoomed area, is also shown the whole data plot and when selection is made in the upper part with the mouse, no changes are shown in the bottom graph; no zoom values are shown, the graphic remain showing the whole data plot.
If I replace the x-axis with auto increased integers, it works perfectly.
What should I do to show and zoom in the datetimes in x-axis?
Thank you in advance!
L.

fig = plt.figure(figsize=(14,6))

ax = fig.add_subplot(211)
x = dfRes.index.to_pydatetime()
y = dfRes['Residual']
ax.plot(x, y, '-')
ax.set_title('Press left mouse button and drag to test')

ax2 = fig.add_subplot(212)
line2, = ax2.plot(x, y, '-')
ax.set_title('...')

def onselect(xmin, xmax):
    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x) - 1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw_idle()

    # save
    np.savetxt("text.out", np.c_[thisx, thisy])

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='red'))

plt.show()

[@tacaswell edited to add markup]

Can you also give us a (synthetic) dataset that reproduces the problem?

I suspect that the problem is that the xmin/xmax that are coming into the callback are in the internal representation of the dates (we have to convert from dates -> floats internally as floats are what the renderers understand). If you want to use these values to select a subset of the data in you will have to un-convert the float back to the “native” units your data is in. Exactly how to do that will depend on which converters are being used, which in turn depends on the exact types that you are passing in.

Hi, @tacaswell
Thank you very much for your reply.
The dataset looks like this:

TimeStamp Residual
2018-01-01 01:00:00-05:00 155.118182
2018-01-01 01:05:00-05:00 87.057639
2018-01-01 01:10:00-05:00 -145.502904
2018-01-01 01:15:00-05:00 -447.563447
2018-01-01 01:20:00-05:00 -445.323990

The column ‘TimeStamp’ contain this type of data:
pandas._libs.tslibs.timestamps.Timestamp

what should I do to properly transform it so that the spanselector shows the dates in the axis and make the zoom in?

Thank you!
L