add_patch() is a lower-level function and updates the data limits (Axes.dataLim) to include the Patch, but doesn't touch the view limits (Axes.viewLim).
scatter() is a high-level function that additionally updates the view limits based on the data limits (essentially through a lazy autoscale_view()), which then picks up the rectangle limits as well.
If you want to have limits exactly on the image, no matter what you plot afterwards you should call ax.autoscale(False) after ax.imshow().
Activity
jklymak commented on Dec 15, 2024
They are all working the same, just scatter is calling autolim, and add_patch is not.
timhoffm commented on Dec 17, 2024
What is happening:
If you want to have limits exactly on the image, no matter what you plot afterwards you should call ax.autoscale(False) after ax.imshow().
timhoffm commented on Dec 17, 2024
I'm closing as the solution for the present case is to deactivate autoscaling.
Independent of this, there are some ideas how to change/improve autoscaling. These are independently tracked, e.g. in