Description
Problem
I would like to plot scatter points as contextual data while preserving the current axes data limits. Something like this can be achieved with Axes.set_autoscale(False)
, but I would like to keep auto-scaling on, and disable it for a particular feature. As far as I understand this is how collections work, which Axes.scatter
uses in the background.
Context: this feature will allow GeoPandas to consistently plot line, polygon (using add_collection()
) and point data (using scatter()
) as contextual data not affecting axes limits (see geopandas/geopandas#2602). Down the line it will benefit a small library I am developing (hyoga), to plot gridded data with xarray, and geographic data for context with geopandas.
Proposed solution
I noticed that add_collection
has an autolim
keyword that defaults to True
. As opposed to completely disabling auto-scaling, setting autolim=False
simply skips updating the axes data limits for this particular collection, meaning auto-scaling stays on, but ignores it (if I read this correctly). I propose to propagate the autolim
keyword argument to Axes.scatter
and pass it to add_collection
here:
matplotlib/lib/matplotlib/axes/_axes.py
Lines 4680 to 4681 in 9d8fde2
This would allow something like:
ax.scatter(x_subject, y_subject) # default to autolim=True, update limits
ax.scatter(x_context, y_context, autolim=False) # don't update limits
ax.plot(something_else) # update limits disregarding {x,y}_context
I am happy to open a PR if this makes sense.