datacarpentry / python-ecology-lesson

Data Analysis and Visualization in Python for Ecologists
https://datacarpentry.org/python-ecology-lesson
Other
160 stars 310 forks source link

correct flood-range-event plot #537

Closed goi42 closed 1 year ago

goi42 commented 1 year ago

Avoids AttributeError raised by previous code snippet. Uploads image that actually corresponds to the code snippet.

Previous code snippet raises error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[16], line 15
     12 fig, ax = plt.subplots()
     13 ax2 = fig.add_axes([0.65, 0.575, 0.25, 0.3])
---> 15 flood.plot(x="datetime", y="flow_rate", ax=ax)
     16 ax2.plot("datetime", "flow_rate", data=discharge)
     17 plt.xticks(rotation=90)

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_core.py:1000, in PlotAccessor.__call__(self, *args, **kwargs)
    997             label_name = label_kw or data.columns
    998             data.columns = label_name
-> 1000 return plot_backend.plot(data, kind=kind, **kwargs)

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_matplotlib/__init__.py:71, in plot(data, kind, **kwargs)
     69         kwargs["ax"] = getattr(ax, "left_ax", ax)
     70 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71 plot_obj.generate()
     72 plot_obj.draw()
     73 return plot_obj.result

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py:455, in MPLPlot.generate(self)
    453 self._add_table()
    454 self._make_legend()
--> 455 self._adorn_subplots()
    457 for ax in self.axes:
    458     self._post_plot_logic_common(ax, self.data)

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py:679, in MPLPlot._adorn_subplots(self)
    677     all_axes = self._get_subplots()
    678     nrows, ncols = self._get_axes_layout()
--> 679     handle_shared_axes(
    680         axarr=all_axes,
    681         nplots=len(all_axes),
    682         naxes=nrows * ncols,
    683         nrows=nrows,
    684         ncols=ncols,
    685         sharex=self.sharex,
    686         sharey=self.sharey,
    687     )
    689 for ax in self.axes:
    690     ax = getattr(ax, "right_ax", ax)

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_matplotlib/tools.py:410, in handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey)
    408 layout = np.zeros((nrows + 1, ncols + 1), dtype=np.bool_)
    409 for ax in axarr:
--> 410     layout[row_num(ax), col_num(ax)] = ax.get_visible()
    412 for ax in axarr:
    413     # only the last row of subplots should get x labels -> all
    414     # other off layout handles the case that the subplot is
    415     # the last in the column, because below is no subplot/gap.
    416     if not layout[row_num(ax) + 1, col_num(ax)]:

File ~/opt/anaconda3/envs/datacarpentry/lib/python3.11/site-packages/pandas/plotting/_matplotlib/tools.py:396, in handle_shared_axes.<locals>.<lambda>(x)
    386 def handle_shared_axes(
    387     axarr: Iterable[Axes],
    388     nplots: int,
   (...)
    393     sharey: bool,
    394 ):
    395     if nplots > 1:
--> 396         row_num = lambda x: x.get_subplotspec().rowspan.start
    397         col_num = lambda x: x.get_subplotspec().colspan.start
    399         if compat.mpl_ge_3_4_0():

AttributeError: 'NoneType' object has no attribute 'rowspan'

In addition, the old image would not have been created by the previous code snippet, even if the error had not been raised.