For a matplotlib that is displayed in an external window (using matplotlib.use('TkAgg')
), the following snippet shows the x/y cursor mouse hover correctly also for a datetime x axis:
import matplotlib
matplotlib.use('TkAgg')
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.plot([datetime.now(), datetime(2020,6,6)], [1,2], "-o")
plt.gca().fmt_xdata = lambda x: mdates.num2date(x).strftime("%Y-%m-%d")
plt.show()
(see: https://stackoverflow.com/a/53650695/10698244)
For Google colab, a workaround exists to show there also the x/y position when hovering over the canvas with the mouse (see: https://stackoverflow.com/a/54907775/10698244):
!pip install mpld3
!pip install "git+https://github.com/javadba/mpld3@display_fix"
import mpld3
from mpld3 import plugins
fig, ax = plt.subplots()
df = pd.DataFrame({"a": [1, 2, 3], "b": [40, 5, 60]})
ax.plot(df, "-o", markersize=2)
plugins.connect(fig, plugins.MousePosition(fontsize=14))
mpld3.display()
Unfortunately, this does not work, when a datetime axis is used. In this case, there is no x/y cursor position shown (if e.g. this dataframe is taken:)
df2 = pd.DataFrame({"a": [pd.datetime(2019,1,12,5,30,1), pd.datetime(2019,1,13,5,30,1), pd.datetime(2019,1,14,5,30,1)], "b": [40, 5, 60]})
df2.set_index("a",inplace=True)
My question is, what has to be done to have also with mpld3
and a datetime x axis the proper x/y cursor positon when hovering over the canvas with the mouse?