Repository: yt-project/yt

Issue:
Annotating grids failed when setting plot width
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

<!--A short 1-2 sentences that succinctly describes the bug-->
`slice.annoate_grids()` failed to plot grids when using `slice.set_width()` or `slice = yt.SlicePlot(ds,'z',"density",width=(400,'km'))`

However, it works with `slice.zoom()`.
(Note: I am using FLASH data)

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

```python
import yt

#
# It works 
#
ds = yt.load("ccsn3d_hdf5_plt_cnt_0656")
slc = yt.SlicePlot(ds,'z',"entr")
slc.zoom(20000/400) # 20000km is the domain size
slc.annotate_grids()
slc.set_log("entr",False)
slc.save('fig_works.png')

#
# IF zoomin by using set_width, it failed to annotate grids
#
ds = yt.load("ccsn3d_hdf5_plt_cnt_0656")
slc = yt.SlicePlot(ds,'z',"entr",width=(400,'km'))
slc.annotate_grids()
slc.set_log("entr",False)
slc.save('fig_failed.png')

```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

```
# If applicable, paste the console output here
#
#
yt : [INFO     ] 2019-07-12 10:34:08,512 Parameters: current_time              = 1.1274643192242684
yt : [INFO     ] 2019-07-12 10:34:08,512 Parameters: domain_dimensions         = [160 160 160]
yt : [INFO     ] 2019-07-12 10:34:08,512 Parameters: domain_left_edge          = [-1.e+09 -1.e+09 -1.e+09]
yt : [INFO     ] 2019-07-12 10:34:08,512 Parameters: domain_right_edge         = [1.e+09 1.e+09 1.e+09]
yt : [INFO     ] 2019-07-12 10:34:08,512 Parameters: cosmological_simulation   = 0.0
yt : [INFO     ] 2019-07-12 10:34:09,760 xlim = -1000000000.000000 1000000000.000000
yt : [INFO     ] 2019-07-12 10:34:09,760 ylim = -1000000000.000000 1000000000.000000
yt : [INFO     ] 2019-07-12 10:34:09,761 xlim = -1000000000.000000 1000000000.000000
yt : [INFO     ] 2019-07-12 10:34:09,761 ylim = -1000000000.000000 1000000000.000000
yt : [INFO     ] 2019-07-12 10:34:09,765 Making a fixed resolution buffer of (('flash', 'entr')) 800 by 800
yt : [INFO     ] 2019-07-12 10:34:10,164 Making a fixed resolution buffer of (('flash', 'entr')) 800 by 800
yt : [WARNING  ] 2019-07-12 10:34:10,299 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,308 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,320 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,330 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,341 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,353 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,366 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,381 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:10,393 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [INFO     ] 2019-07-12 10:34:10,399 Saving plot fig_works.png
yt : [INFO     ] 2019-07-12 10:34:10,793 Parameters: current_time              = 1.1274643192242684
yt : [INFO     ] 2019-07-12 10:34:10,793 Parameters: domain_dimensions         = [160 160 160]
yt : [INFO     ] 2019-07-12 10:34:10,794 Parameters: domain_left_edge          = [-1.e+09 -1.e+09 -1.e+09]
yt : [INFO     ] 2019-07-12 10:34:10,794 Parameters: domain_right_edge         = [1.e+09 1.e+09 1.e+09]
yt : [INFO     ] 2019-07-12 10:34:10,794 Parameters: cosmological_simulation   = 0.0
yt : [INFO     ] 2019-07-12 10:34:11,967 xlim = -200.000000 200.000000
yt : [INFO     ] 2019-07-12 10:34:11,967 ylim = -200.000000 200.000000
yt : [INFO     ] 2019-07-12 10:34:11,968 xlim = -200.000000 200.000000
yt : [INFO     ] 2019-07-12 10:34:11,968 ylim = -200.000000 200.000000
yt : [INFO     ] 2019-07-12 10:34:11,968 Making a fixed resolution buffer of (('flash', 'entr')) 800 by 800
yt : [WARNING  ] 2019-07-12 10:34:12,288 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,297 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,307 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,318 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,328 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,337 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,351 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,362 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [WARNING  ] 2019-07-12 10:34:12,373 Supplied id_loc but draw_ids is False. Not drawing grid ids
yt : [INFO     ] 2019-07-12 10:34:12,374 Saving plot fig_failed.png
(python3) ------------------------------------------------------------

```
`fig_works.png` : https://www.dropbox.com/s/d02wbxzg52reye6/fig_works.png?dl=0
`fig_failed.png`: https://www.dropbox.com/s/vnybxlbfjd0u85t/fig_failed.png?dl=0


**Expected outcome**

<!--A description of the expected outcome from the code snippet-->
<!--If this used to work in an earlier version of yt, please note the
version it used to work on-->

`fig_failed.png` and `fig_works.png` should be identical.
It was working with the previous version of `yt`.

**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: CentOS 7 or Mac OS X
  * Python Version: 3.6
  * yt version: 3.6.dev0
  * Other Libraries (if applicable): 

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->

I installed `yt` from the nightly built version through anaconda.



Discussion:
FLASH data can be found here: https://www.dropbox.com/s/t7lkor3cfn1ktit/ccsn3d_hdf5_plt_cnt_0656?dl=0
I just want to report evidence that this is likely a frontend-specific issue.
I tried to reproduce with fake data, here's the outcome

```python
import yt
from yt.testing import fake_amr_ds

ds = fake_amr_ds()
slc = yt.SlicePlot(ds, 'z', 'Density')
slc.annotate_grids()
slc.set_cmap('all', 'arbre')
slc.save('/tmp/nozoom')

ds = fake_amr_ds()
slc = yt.SlicePlot(ds, 'z', 'Density', width=(7,'mm'))
slc.annotate_grids()
slc.set_cmap('all', 'arbre')
slc.save('/tmp/zoom')
```
![nozoom_Slice_z_Density](https://user-images.githubusercontent.com/14075922/78510121-46cd9f00-7793-11ea-80bd-03ae07958e5e.png)
![zoom_Slice_z_Density](https://user-images.githubusercontent.com/14075922/78510128-4d5c1680-7793-11ea-8850-42279b41e762.png)



Resolving patch:
diff --git a/yt/visualization/plot_modifications.py b/yt/visualization/plot_modifications.py
--- a/yt/visualization/plot_modifications.py
+++ b/yt/visualization/plot_modifications.py
@@ -250,16 +250,20 @@ def _sanitize_coord_system(self, plot, coord, coord_system):
             raise SyntaxError("Argument coord_system must have a value of "
                               "'data', 'plot', 'axis', or 'figure'.")
 
-    def _pixel_scale(self, plot):
-        x0, x1 = np.array(plot.xlim)
-        xx0, xx1 = plot._axes.get_xlim()
-        dx = (xx1 - xx0)/(x1 - x0)
+    def _physical_bounds(self, plot):
+        xlims = tuple(v.in_units("code_length") for v in plot.xlim)
+        ylims = tuple(v.in_units("code_length") for v in plot.ylim)
+        return xlims + ylims
 
-        y0, y1 = np.array(plot.ylim)
-        yy0, yy1 = plot._axes.get_ylim()
-        dy = (yy1 - yy0)/(y1 - y0)
+    def _plot_bounds(self, plot):
+        return plot._axes.get_xlim() + plot._axes.get_ylim()
 
-        return (dx,dy)
+    def _pixel_scale(self, plot):
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
+        dx = (xx1 - xx0) / (x1 - x0)
+        dy = (yy1 - yy0) / (y1 - y0)
+        return dx, dy
 
     def _set_font_properties(self, plot, labels, **kwargs):
         """
@@ -460,10 +464,8 @@ def __init__(self, field_x, field_y, factor=16, scale=None,
         self.plot_args = plot_args
 
     def __call__(self, plot):
-        x0, x1 = [p.to('code_length') for p in plot.xlim]
-        y0, y1 = [p.to('code_length') for p in plot.ylim]
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         bounds = [x0,x1,y0,y1]
         periodic = int(any(plot.data.ds.periodicity))
 
@@ -554,12 +556,10 @@ def __call__(self, plot):
         from matplotlib.tri import Triangulation, LinearTriInterpolator
 
         # These need to be in code_length
-        x0, x1 = (v.in_units("code_length") for v in plot.xlim)
-        y0, y1 = (v.in_units("code_length") for v in plot.ylim)
+        x0, x1, y0, y1 = self._physical_bounds(plot)
 
         # These are in plot coordinates, which may not be code coordinates.
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
 
         # See the note about rows/columns in the pixelizer for more information
         # on why we choose the bounds we do
@@ -679,10 +679,8 @@ def __call__(self, plot):
                 for 2D cylindrical geometry, not 3D")
         from matplotlib.colors import colorConverter
 
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         (dx, dy) = self._pixel_scale(plot)
         (ypix, xpix) = plot.image._A.shape
         ax = plot.data.axis
@@ -816,11 +814,8 @@ def __init__(self, field_x, field_y, factor=16,
         self.plot_args = plot_args
 
     def __call__(self, plot):
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
-        bounds = [x0,x1,y0,y1]
+        bounds = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
 
         # We are feeding this size into the pixelizer, where it will properly
         # set it in reverse order
@@ -946,8 +941,7 @@ def __call__(self, plot):
                             coord_system=self.coord_system)
         p2 = self._sanitize_coord_system(plot, self.p2,
                             coord_system=self.coord_system)
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         plot._axes.plot([p1[0], p2[0]], [p1[1], p2[1]], transform=self.transform,
                         **self.plot_args)
         plot._axes.set_xlim(xx0,xx1)
@@ -1002,10 +996,8 @@ def __init__(self, field_x, field_y, factor=16, scale=None,
         self.plot_args = plot_args
 
     def __call__(self, plot):
-        x0, x1 = [p.to('code_length') for p in plot.xlim]
-        y0, y1 = [p.to('code_length') for p in plot.ylim]
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         nx = plot.image._A.shape[1] // self.factor
         ny = plot.image._A.shape[0] // self.factor
         indices = np.argsort(plot.data['dx'])[::-1].astype(np.int_)
@@ -1057,12 +1049,8 @@ def __init__(self, clumps, plot_args=None):
         self.plot_args = plot_args
 
     def __call__(self, plot):
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
-
-        extent = [xx0,xx1,yy0,yy1]
+        bounds = self._physical_bounds(plot)
+        extent = self._plot_bounds(plot)
 
         ax = plot.data.axis
         px_index = plot.data.ds.coordinates.x_axis[ax]
@@ -1092,10 +1080,10 @@ def __call__(self, plot):
 
             temp = np.zeros((ny, nx), dtype="f8")
             pixelize_cartesian(temp, xf_copy, yf_copy,
-                                 clump[ftype, dxf].in_units("code_length")/2.0,
-                                 clump[ftype, dyf].in_units("code_length")/2.0,
-                                 clump[ftype, dxf].d*0.0+i+1, # inits inside Pixelize
-                             (x0, x1, y0, y1), 0)
+                               clump[ftype, dxf].in_units("code_length")/2.0,
+                               clump[ftype, dyf].in_units("code_length")/2.0,
+                               clump[ftype, dxf].d*0.0+i+1, # inits inside Pixelize
+                               bounds, 0)
             buff = np.maximum(temp, buff)
         self.rv = plot._axes.contour(buff, np.unique(buff),
                                      extent=extent, **self.plot_args)
@@ -1202,8 +1190,7 @@ def __init__(self, pos, code_size=None, length=0.03, width=0.0001,
     def __call__(self, plot):
         x,y = self._sanitize_coord_system(plot, self.pos,
                                coord_system=self.coord_system)
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         # normalize all of the kwarg lengths to the plot size
         plot_diag = ((yy1-yy0)**2 + (xx1-xx0)**2)**(0.5)
         self.length *= plot_diag
@@ -1316,8 +1303,7 @@ def __init__(self, pos, marker='x', coord_system="data", plot_args=None):
     def __call__(self, plot):
         x,y = self._sanitize_coord_system(plot, self.pos,
                                coord_system=self.coord_system)
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         plot._axes.scatter(x, y, marker = self.marker,
                            transform=self.transform, **self.plot_args)
         plot._axes.set_xlim(xx0,xx1)
@@ -1415,8 +1401,7 @@ def __call__(self, plot):
 
         cir = Circle((x, y), self.radius, transform=self.transform,
                      **self.circle_args)
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
 
         plot._axes.add_patch(cir)
         if self.text is not None:
@@ -1512,8 +1497,7 @@ def __call__(self, plot):
 
         # Set the font properties of text from this callback to be
         # consistent with other text labels in this figure
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         if self.inset_box_args is not None:
             kwargs['bbox'] = self.inset_box_args
         label = plot._axes.text(x, y, self.text, transform=self.transform,
@@ -1675,10 +1659,8 @@ def __init__(self, halo_catalog, circle_args=None, circle_kwargs=None,
     def __call__(self, plot):
         from matplotlib.patches import Circle
         data = plot.data
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
 
         halo_data = self.halo_data
         axis_names = plot.data.ds.coordinates.axis_name
@@ -1794,12 +1776,8 @@ def __call__(self, plot):
         else:
             self.width = plot.data.ds.quan(self.width, "code_length")
         # we construct a rectangular prism
-        x0 = plot.xlim[0].to("code_length")
-        x1 = plot.xlim[1].to("code_length")
-        y0 = plot.ylim[0].to("code_length")
-        y1 = plot.ylim[1].to("code_length")
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         if type(self.data_source)==YTCutRegion:
             mylog.warning("Parameter 'width' is ignored in annotate_particles if the "
                           "data_source is a cut_region. "
@@ -2647,12 +2625,8 @@ def __init__(self, field_x, field_y, texture=None, kernellen=50.,
 
     def __call__(self, plot):
         from matplotlib import cm
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
-        bounds = [x0,x1,y0,y1]
-        extent = [xx0,xx1,yy0,yy1]
+        bounds = self._physical_bounds(plot)
+        extent = self._plot_bounds(plot)
 
         # We are feeding this size into the pixelizer, where it will properly
         # set it in reverse order
@@ -2742,10 +2716,8 @@ def __call__(self, plot):
             plot.data.ds.dimensionality == 3:
             raise NotImplementedError("Cell edge annotation is only supported for \
                 for 2D cylindrical geometry, not 3D")
-        x0, x1 = plot.xlim
-        y0, y1 = plot.ylim
-        xx0, xx1 = plot._axes.get_xlim()
-        yy0, yy1 = plot._axes.get_ylim()
+        x0, x1, y0, y1 = self._physical_bounds(plot)
+        xx0, xx1, yy0, yy1 = self._plot_bounds(plot)
         nx = plot.image._A.shape[1]
         ny = plot.image._A.shape[0]
         aspect = float((y1 - y0) / (x1 - x0))


Test patch: