Skip to content

New errors with v.3.3.1 using tight_layout() and constrained_layout #18319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anissa111 opened this issue Aug 21, 2020 · 2 comments
Closed

New errors with v.3.3.1 using tight_layout() and constrained_layout #18319

anissa111 opened this issue Aug 21, 2020 · 2 comments

Comments

@anissa111
Copy link

anissa111 commented Aug 21, 2020

Bug report

Bug summary

Previously working example documentation code using tight_layout() and constrained_layout now produces errors.

Code for reproduction

One of the failing codes (github link):

"""
NCL_stream_1.py
===============
This script illustrates the following concepts:
   - Drawing a black-and-white streamline plot over a map

See following URLs to see the reproduced NCL plot & script:
    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/stream_1.ncl
    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/stream_1_lg.png

Note: The streamlines generated in this plot approximate, but do not exactly
match those generated by the NCL version. This is primarily because the 
seed point generation algorithm used by NCL is opaque.
"""

################################################################################
# Import packages:
import numpy as np
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature

import geocat.datafiles as gdf
from geocat.viz import util as gvutil

################################################################################
# Read in data:

# Open a netCDF data file using xarray default engine and load the data into xarrays
ds = xr.open_dataset(gdf.get('netcdf_files/uvt.nc'))
# Extract a 2D horizontal slice from the first time step of the 3D U and V variables at the bottom level
U = ds.U.isel(time=0, lev=0)
V = ds.V.isel(time=0, lev=0)

################################################################################
# Plot:

# Generate figure (set its size (width, height) in inches)
plt.figure(figsize=(16, 8))

# Generate axes using Cartopy projection
projection = ccrs.PlateCarree()
ax = plt.axes(projection=projection)

# Use global map
ax.set_global()

# Stream-plot the data
# There is no Xarray streamplot function, yet. So need to call matplotlib.streamplot directly. Not sure why, but can't
# pass xarray.DataArray objects directly: fetch NumPy arrays via 'data' attribute'
ax.streamplot(U.lon.data,
              U.lat.data,
              U.data,
              V.data,
              linewidth=1,
              density=4,
              color='black',
              zorder=1)


# Show the plot
plt.tight_layout()
plt.show()

Actual outcome

Traceback (most recent call last):
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-32ee43073f80>", line 1, in <module>
    runfile('/Users/anissaz/Desktop/GeoCAT/GeoCAT-examples/Plots/Streamlines/NCL_stream_1.py', wdir='/Users/anissaz/Desktop/GeoCAT/GeoCAT-examples/Plots/Streamlines')
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/anissaz/Desktop/GeoCAT/GeoCAT-examples/Plots/Streamlines/NCL_stream_1.py", line 64, in <module>
    plt.show()
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/pyplot.py", line 337, in show
    return _backend_mod.show(*args, **kwargs)
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 29, in __call__
    manager.show(**kwargs)
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 113, in show
    self.canvas.show()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 68, in show
    self.figure.tight_layout()
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/figure.py", line 2615, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/tight_layout.py", line 308, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/tight_layout.py", line 84, in auto_adjust_subplotpars
    bb += [ax.get_tightbbox(renderer, for_layout_only=True)]
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 4203, in get_tightbbox
    bbox = a.get_tightbbox(renderer)
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/artist.py", line 286, in get_tightbbox
    bbox = Bbox.intersection(bbox, clip_path.get_extents())
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/matplotlib/path.py", line 603, in get_extents
    return Bbox([xys.min(axis=0), xys.max(axis=0)])
  File "/opt/anaconda3/envs/geocat-examples/lib/python3.7/site-packages/numpy/core/_methods.py", line 43, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity

Here is a link to one of our current unmerged PRs with sphinx documentation generation failures for more than just this example, if that is helpful.

Expected outcome
Used to produce the following image:
image

Previously working example documented with readthedocs here

Version 3.3.0 was the last version that did not experience this.

Matplotlib version

  • Operating system: MacOS (10.14.6)
  • Matplotlib version: 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): module://backend_interagg
  • Platform version: 3.7.8
  • Python version: Python 3.7.8
  • Numpy version: 1.19.1
  • Xarray version: 0.16.0
  • Cartopy version: 0.18.0

Installed matplotlib from conda environment

@QuLogic
Copy link
Member

QuLogic commented Aug 22, 2020

Can you a) reduce the example to not use extra data, and b) reproduce this without using PyCharm's custom backend?

@QuLogic QuLogic added the status: needs clarification Issues that need more information to resolve. label Aug 22, 2020
@QuLogic
Copy link
Member

QuLogic commented Aug 22, 2020

Actually, never mind, I looked closer at the linked build. This is a duplicate of #18254 and will be fixed in 3.3.2.

@QuLogic QuLogic closed this as completed Aug 22, 2020
@QuLogic QuLogic added status: duplicate and removed status: needs clarification Issues that need more information to resolve. labels Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants