This is a very direct follow-up on this question.
Using matplotlib, I’d like to be able to place a sort of “highlighting bar” over a range of data markers that I know will all be in a straight horizontal line.
This bar/rectangle should be slightly taller than the markers and contain them, something like this for the three markers below:

In order to be a sensible highlighting bar, it needs to have the following two traits:
- If the plot is panned, the bar moves with the markers (so it always covers them).
- If the plot is zoomed, the bar’s display height doesn’t change (so it always is slightly taller than the markers).
If it is helpful to know, these markers have no meaningful y values (they are plotted all at y=-1), only meaningful x values. Therefore, the height of the bar is meaningless in data coordinates; it merely needs to be always just tall enough to enclose the markers.
Great question! This was a good challenge and requires a combination of things to achieve.
Firstly, we need to invent a transform which will return the device coordinates of a pre-defined value plus an offset based on the given point. For instance, if we know we want the bar to be at x_pt, y_pt, then the transform should represent (in pseudo code):
Once we have done this, we could use this transform to draw a box of 20 pixels around a fixed data point. However, you only want to draw a box of fixed pixel height in the y direction, but in the x direction you would like standard data scaling.
Therefore, we need to create a blended transform which can transform the x and y coordinates independently. The whole code to do what you are asking: