I have a graph, computed from some data, drawn in matplotlib. I want to draw a rectangular region around the global maximum of this graph. I tried plt.axhspan, but the rectangle doesn’t seem to appear when I call plt.show()
So, how can a rectangular region be drawn onto a matplotlib graph? Thanks!
The most likely reason is that you used data units for the x arguments when calling axhspan. From the function’s docs (my emphasis):
So any rectangle stretching left of 0 or right of 1 is simply drawn off-plot.
An easy alternative might be to add a
Rectangleto your axis (e.g., viaplt.gcaandadd_patch);Rectangleuses data units for both dimensions. The following would add a grey rectangle with width & height of 1 centered on (2,3):facecolor