I have a plot where in abscissa I have the year 2022, and in ordinate a function of time
In [215]: import matplotlib.pyplot as plt
...: from matplotlib.dates import MonthLocator, DateFormatter
...: from numpy import linspace, sin
...:
...: T0 = (2022-1970)*365.25 ; T1 = T0 365 ; T = np.linspace(T0, T1, 366)
...: w = 6.28/40
...:
...: fig, ax = plt.subplots(figsize=(14,5), constrained_layout=1)
...: ax.plot(T,40 15*np.sin(w*T-0.2))
...: ax.xaxis.set_major_locator(MonthLocator(bymonth=range(13)))
...: ax.xaxis.set_major_formatter(DateFormatter("%b %Y"))
...:
...: plt.show()
As you can see from the code, the function is plotted with a one day resolution, but because of my choice for the formatting of x-tick-labels when I move the mouse over the interactive window what I see in the upper right corner is just, e.g., "x=May 2022 y=43.31".
Is it possible to have a different formatter for the interactive window display of the mouse position?
CodePudding user response:
You could even go one step further, and override the ax.format_coord function. This is a function, given an x and y value, to produce the full coordinate string to be displayed in the status bar. By default, it just combines ax.fmt_xdata
and ax.fmt_ydata
.
Also the mplcursors library might be of interest. With it, you can obtain the xy coordinates of the nearest point on the curve, and show that information as an annotation, or as overriding the status bar.