Repository: sunpy/sunpy

Issue:
Need example of how to use xlim, ylim when plotting with WCS axes
### Description
I always have to think twice (thrice) when using `ax.set_xlim` and `ax.set_ylim` with plotting with maps and WCSAxes, and it's not really that straight forward.

An example of this would be good to include - I'll make a PR now unless it exists somewhere? (I dont think so).

Also is this the easiest way to do it? i.e. find pixel coordinates and use those or is there some WCSaxes magic to do `transform=` or something?

```python
xlim = [-500, 500]*u.arcsec
ylim = [-800, 800]*u.arcsec

limits = aia_map.world_to_pixel(SkyCoord(Tx=xlim, Ty=ylim, frame=aia_map.coordinate_frame))
...

ax.set_xlim(limits.x.value)
ax.set_ylim(limits.y.value)

```




Discussion:
(sorry I'm repeating stuff you already know here!)

Whenever a map is plotted, the x/y axes that Matplotlib (and therefore `set_{x,y}lim`) uses are always pixel axes. On top of that, `WCSAxes` then "decorates" the axes with the world coordinates and coordinate grid. Because in the general case there is an arbitrary non-linear transformation between pixel and world coordinates, it's not (in general) possible to set plot limits with world coordinates.

In practice though, world and pixel coordinates are often aligned, so one can effectively work out limits by transforming world coordinates to pixel coordinates, which is what you're doing. As far as I know your code snippet is the best way to do this.

I agree that an example explaining the above, and having your code would be great 👍 
Putting my wcsaxes :tophat: on for a moment.

I feel like there is no reason why *in theory* wcsaxes couldn't do something to make this easier. I do wonder if you could either define the x limits along the x spine (i.e. you give it two 2D coordinate points which it uses to crop the x axis) and then do the same for the y axis, or you could do a similar rectangle like API to submap to crop the box based on showing all for corners.

Resolving patch:
diff --git a/examples/plotting/xy_lims.py b/examples/plotting/xy_lims.py
new file mode 100644
--- /dev/null
+++ b/examples/plotting/xy_lims.py
@@ -0,0 +1,56 @@
+"""
+==================================
+Set Axis Range When Plotting a Map
+==================================
+
+In this example we are going to look at how to set the axes
+range using Matplotlib's `set_xlim` and `set_ylim` when plotting a
+Map with WCSAxes.
+"""
+
+import matplotlib.pyplot as plt
+
+from astropy import units as u
+from astropy.coordinates import SkyCoord
+
+import sunpy.data.sample
+import sunpy.map
+
+###############################################################################
+# Lets start by creating a Map from the sample data.
+aia_map = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE)
+
+###############################################################################
+# Now lets say for example we are only interested in plotting a certain region
+# of this Map. One way this could be done is to create a submap over the region
+# of interest and then plotting that. Another useful way is to set the axes
+# range over which to plot using Matplotlib's `Axes.set_xlim` and `Axes.set_ylim` functionality.
+# The axes that Matplotlib uses is in pixel coordinates (e.g. of image data array)
+# rather than world coordinates (e.g. in arcsecs) so we need to define our limits that
+# are passed to set_xlim(), set_lim() to pixel coordinates.
+# We can define our limits we want to use in world coordinates and then work out what pixel
+# coordinates these correspond to.
+# Lets choose xlimits and ylimits in arcsecs that we are interested in.
+xlims_world = [500, 1100]*u.arcsec
+ylims_world = [-800, 0]*u.arcsec
+
+###############################################################################
+# We can then covert these into a SkyCoord which can be passed to :func:`~sunpy.map.GenericMap.world_to_pixel` to
+# determine which pixel coordinates these represent on the Map.
+world_coords = SkyCoord(Tx=xlims_world, Ty=ylims_world, frame=aia_map.coordinate_frame)
+pixel_coords = aia_map.world_to_pixel(world_coords)
+
+# we can then pull out the x and y values of these limits.
+xlims_pixel = pixel_coords.x.value
+ylims_pixel = pixel_coords.y.value
+
+###############################################################################
+# We can now plot this Map and then use the x_lims_pixel and y_lims_pixel to set
+# the range of the axes for which to plot.
+fig = plt.figure()
+ax = plt.subplot(projection=aia_map)
+aia_map.plot(axes=ax, clip_interval=(1, 99.9)*u.percent)
+
+ax.set_xlim(xlims_pixel)
+ax.set_ylim(ylims_pixel)
+plt.show()


Test patch: