Skip to content
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

emsarray plotting error #4

Closed
Dinga1990 opened this issue Sep 17, 2023 · 3 comments
Closed

emsarray plotting error #4

Dinga1990 opened this issue Sep 17, 2023 · 3 comments

Comments

@Dinga1990
Copy link

Dinga1990 commented Sep 17, 2023

I'm trying to plot using emsarray and getting the following issue. I have update my libraries and now I'm not sure why this is occuring. I can use other plotting without any issues. I was following https://github.com/csiro-coasts/emsarray-notebooks/blob/master/plot.ipynb

Input Code:

import emsarray
from matplotlib import pyplot as plt
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (8, 5)
plt.rcParams['savefig.dpi'] = 100
gbr4 = emsarray.tutorial.open_dataset('gbr4')
gbr4.ems.plot('botz', title='Depth of sea-bed')

Error received.

**AttributeError**
Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\IPython\core\formatters.py:338, in BaseFormatter.__call__(self, obj)
    336     pass
    337 else:
--> 338     return printer(obj)
    339 # Finally look for special method names
    340 method = get_real_method(obj, self.print_method)

File ~\anaconda3\lib\site-packages\IPython\core\pylabtools.py:152, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
    149     from matplotlib.backend_bases import FigureCanvasBase
    150     FigureCanvasBase(fig)
--> 152 fig.canvas.print_figure(bytes_io, **kw)
    153 data = bytes_io.getvalue()
    154 if fmt == 'svg':

File ~\AppData\Roaming\Python\Python310\site-packages\matplotlib\backend_bases.py:2346, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2344 if bbox_inches:
   2345     if bbox_inches == "tight":
-> 2346         bbox_inches = self.figure.get_tightbbox(
   2347             renderer, bbox_extra_artists=bbox_extra_artists)
   2348         if pad_inches is None:
   2349             pad_inches = rcParams['savefig.pad_inches']

File ~\AppData\Roaming\Python\Python310\site-packages\matplotlib\figure.py:1776, in FigureBase.get_tightbbox(self, renderer, bbox_extra_artists)
   1773     artists = bbox_extra_artists
   1775 for a in artists:
-> 1776     bbox = a.get_tightbbox(renderer)
   1777     if bbox is not None:
   1778         bb.append(bbox)

File ~\AppData\Roaming\Python\Python310\site-packages\matplotlib\artist.py:367, in Artist.get_tightbbox(self, renderer)
    352 def get_tightbbox(self, renderer=None):
    353     """
    354     Like `.Artist.get_window_extent`, but includes any clipping.
    355 
   (...)
    365         The enclosing bounding box (in figure pixel coordinates).
    366     """
--> 367     bbox = self.get_window_extent(renderer)
    368     if self.get_clip_on():
    369         clip_box = self.get_clip_box()

File ~\AppData\Roaming\Python\Python310\site-packages\matplotlib\text.py:943, in Text.get_window_extent(self, renderer, dpi)
    941     return Bbox.unit()
    942 if dpi is None:
--> 943     dpi = self.figure.dpi
    944 if self.get_text() == '':
    945     with cbook._setattr_cm(self.figure, dpi=dpi):

**AttributeError:** 'NoneType' object has no attribute 'dpi'

<Figure size 800x500 with 2 Axes>
@mx-moth
Copy link
Contributor

mx-moth commented Sep 19, 2023

I can reproduce this locally by installing the latest versions of everything and running in jupyter lab.

$ conda install -c conda-forge emsarray jupyter-lab ipympl

I replaced the ds.ems.plot() code with the actual implementation of the functions and found the issue is with the grid lines. I have not yet worked out a suitable fix, work around, or replacement for the problem bit of code though.

%matplotlib ipympl

import emsarray
import numpy
from matplotlib import pyplot as plt

ds = emsarray.tutorial.open_dataset('gbr4')

figure = plt.figure(figsize=(8, 6), dpi=100)
axes = figure.add_subplot(projection=ds.ems.data_crs)
axes.set_aspect(aspect='equal', adjustable='datalim')
axes.set(title="botz")

axes.add_collection(ds.ems.make_poly_collection(ds['botz'], cmap='jet', edgecolor='face'))

gridlines = axes.gridlines(draw_labels=["bottom", "left"], auto_update=True)

axes.autoscale()

Either setting auto_update=False or removing the draw_labels line will fix the issue and allow the plot to render. However removing auto_update=True causes the grid lines to be misaligned, and having labels on the axes is desired.

@mx-moth
Copy link
Contributor

mx-moth commented Sep 19, 2023

This issue and specifically this comment seems relevant: SciTools/cartopy#1541 (comment)

@mx-moth
Copy link
Contributor

mx-moth commented Jul 11, 2024

This issue is resolved in more recent versions of emsarray (see csiro-coasts/emsarray#137) and cartopy (see SciTools/cartopy#2246).

@mx-moth mx-moth closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants