This function provides interactive crosshairs on matplotlib plots. The crosshairs will follow the users’ mouse cursor when they are near lines or points and will snap to these lines and points. Upon a mouse click the crosshairs will add an annotation to the plot. This annotation can be dragged to a new position. To delete the annotation, right click on it. To temporarily hide all annotations, toggle ‘h’ on your keyboard.

Note that crosshairs should be called after everything is added to the plot (but before plt.show()) so that the objects in the plot are identified for the ‘snap to’ feature. If something is added to the plot after calling crosshairs then you will not be able to move the crosshairs onto it.

If your interactive development environment does not generate the plot in its own window then your plot is not interactive and this will not work. For iPython notebook users, the interactive window should be available by typing “%matplotlib qt” after importing matplotlib as described here.

There are some customisable attributes of the crosshairs and annotations using the following inputs:

  • xlabel - the x-label for the annotation. Default is x.
  • ylabel - the y-label for the annotation. Default is y.
  • decimals - the number of decimals to use when rounding values in the crosshairs and in the annotation. Default is 2.
  • dateformat - the datetime format. If specified the x crosshair and label will be formatted as a date using the format provided. Default is None which results in no date format being used on x. For a list of acceptable dateformat strings see strftime.
  • plotting kwargs are also accepted. eg. color, linestyle, etc.

In the following example, we see the crosshairs being used to display the value of the Weibull CDF. The dynamic nature of this feature is shown in the video at the bottom of this page.

from reliability.Other_functions import crosshairs
from reliability.Distributions import Weibull_Distribution
import matplotlib.pyplot as plt

crosshairs(xlabel='t',ylabel='F') #it is important to call this last

A special thanks goes to Antony Lee, the author of mplcursors. The crosshairs function works using mplcursors to enable the ‘snap to’ feature and the annotations. Antony was very helpful in getting this to work.