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]