# Buggy code
def _set_psd_plot_params(info, proj, picks, ax, area_mode):
    """Set PSD plot params."""
    import matplotlib.pyplot as plt

    _check_option("area_mode", area_mode, [None, "std", "range"])
    picks = _picks_to_idx(info, picks)

    # XXX this could be refactored more with e.g., plot_evoked
    # XXX when it's refactored, Report._render_raw will need to be updated
    titles = _handle_default("titles", None)
    units = _handle_default("units", None)
    scalings = _handle_default("scalings", None)
    picks_list = list()
    titles_list = list()
    units_list = list()
    scalings_list = list()
    for name in _DATA_CH_TYPES_SPLIT:
        kwargs = dict(meg=False, ref_meg=False, exclude=[])
        if name in ("mag", "grad"):
            kwargs["meg"] = name
        elif name in ("fnirs_raw", "fnirs_od", "hbo", "hbr"):
            kwargs["fnirs"] = name
        else:
            kwargs[name] = True
        these_picks = pick_types(info, **kwargs)
        these_picks = np.intersect1d(these_picks, picks)
        if len(these_picks) > 0:
            picks_list.append(these_picks)
            titles_list.append(titles[name])
            units_list.append(units[name])
            scalings_list.append(scalings[name])
    if len(picks_list) == 0:
        raise RuntimeError("No data channels found")
    if ax is not None:
        if isinstance(ax, plt.Axes):
            ax = [ax]
        if len(ax) != len(picks_list):
            raise ValueError(
                "For this dataset with picks=None %s axes "
                "must be supplied, got %s" % (len(picks_list), len(ax))
            )
        ax_list = ax
    del picks

    fig = None
    if ax is None:
        fig = plt.figure()
        ax_list = list()
        for ii in range(len(picks_list)):
            # Make x-axes change together
            if ii > 0:
                ax_list.append(
                    plt.subplot(len(picks_list), 1, ii + 1, sharex=ax_list[0])
                )
            else:
                ax_list.append(plt.subplot(len(picks_list), 1, ii + 1))
        make_label = True
    else:
        fig = ax_list[0].get_figure()
        make_label = len(ax_list) == len(fig.axes)

    return (
        fig,
        picks_list,
        titles_list,
        units_list,
        scalings_list,
        ax_list,
        make_label,
    )


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
339                 pass
340             else:
--> 341                 return printer(obj)
342             # Finally look for special method names
343             method = get_real_method(obj, self.print_method)

~/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
242
243     if 'png' in formats:
--> 244         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
245     if 'retina' in formats or 'png2x' in formats:
246         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
126
127     bytes_io = BytesIO()
--> 128     fig.canvas.print_figure(bytes_io, **kw)
129     data = bytes_io.getvalue()
130     if fmt == 'svg':

~/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2080                     orientation=orientation,
2081                     bbox_inches_restore=_bbox_inches_restore,
-> 2082                     **kwargs)
2083             finally:
2084                 if bbox_inches and restore_bbox:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
525
526         else:
--> 527             FigureCanvasAgg.draw(self)
528             renderer = self.get_renderer()
529             with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in draw(self)
384         Draw the figure using the renderer.
385         """
--> 386         self.renderer = self.get_renderer(cleared=True)
387         with RendererAgg.lock:
388             self.figure.draw(self.renderer)

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
397                           and getattr(self, "_lastKey", None) == key)
398         if not reuse_renderer:
--> 399             self.renderer = RendererAgg(w, h, self.figure.dpi)
400             self._lastKey = key
401         elif cleared:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
84         self.width = width
85         self.height = height
---> 86         self._renderer = _RendererAgg(int(width), int(height), dpi)
87         self._filter_renderers = []
88

ValueError: Image size of 115104x291 pixels is too large. It must be less than 2^16 in each direction.

<Figure size 432x288 with 6 Axes>

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
339                 pass
340             else:
--> 341                 return printer(obj)
342             # Finally look for special method names
343             method = get_real_method(obj, self.print_method)

~/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
242
243     if 'png' in formats:
--> 244         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
245     if 'retina' in formats or 'png2x' in formats:
246         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
126
127     bytes_io = BytesIO()
--> 128     fig.canvas.print_figure(bytes_io, **kw)
129     data = bytes_io.getvalue()
130     if fmt == 'svg':

~/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2080                     orientation=orientation,
2081                     bbox_inches_restore=_bbox_inches_restore,
-> 2082                     **kwargs)
2083             finally:
2084                 if bbox_inches and restore_bbox:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
525
526         else:
--> 527             FigureCanvasAgg.draw(self)
528             renderer = self.get_renderer()
529             with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in draw(self)
384         Draw the figure using the renderer.
385         """
--> 386         self.renderer = self.get_renderer(cleared=True)
387         with RendererAgg.lock:
388             self.figure.draw(self.renderer)

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
397                           and getattr(self, "_lastKey", None) == key)
398         if not reuse_renderer:
--> 399             self.renderer = RendererAgg(w, h, self.figure.dpi)
400             self._lastKey = key
401         elif cleared:

~/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
84         self.width = width
85         self.height = height
---> 86         self._renderer = _RendererAgg(int(width), int(height), dpi)
87         self._filter_renderers = []
88

ValueError: Image size of 115104x291 pixels is too large. It must be less than 2^16 in each direction.

# Fixed code
def _set_psd_plot_params(info, proj, picks, ax, area_mode):
    """Set PSD plot params."""
    import matplotlib.pyplot as plt

    _check_option("area_mode", area_mode, [None, "std", "range"])
    picks = _picks_to_idx(info, picks)

    # XXX this could be refactored more with e.g., plot_evoked
    # XXX when it's refactored, Report._render_raw will need to be updated
    titles = _handle_default("titles", None)
    units = _handle_default("units", None)
    scalings = _handle_default("scalings", None)
    picks_list = list()
    titles_list = list()
    units_list = list()
    scalings_list = list()
    for name in _DATA_CH_TYPES_SPLIT:
        kwargs = dict(meg=False, ref_meg=False, exclude=[])
        if name in ("mag", "grad"):
            kwargs["meg"] = name
        elif name in ("fnirs_raw", "fnirs_od", "hbo", "hbr"):
            kwargs["fnirs"] = name
        else:
            kwargs[name] = True
        these_picks = pick_types(info, **kwargs)
        these_picks = np.intersect1d(these_picks, picks)
        if len(these_picks) > 0:
            picks_list.append(these_picks)
            titles_list.append(titles[name])
            units_list.append(units[name])
            scalings_list.append(scalings[name])
    if len(picks_list) == 0:
        raise RuntimeError("No data channels found")
    if ax is not None:
        if isinstance(ax, plt.Axes):
            ax = [ax]
        if len(ax) != len(picks_list):
            raise ValueError(
                "For this dataset with picks=None %s axes "
                "must be supplied, got %s" % (len(picks_list), len(ax))
            )
        ax_list = ax
    del picks

    fig = None
    if ax is None:
        fig, ax_list = plt.subplots(len(picks_list), 1, sharex=True, squeeze=False)
        ax_list = list(ax_list[:, 0])
        make_label = True
    else:
        fig = ax_list[0].get_figure()
        make_label = len(ax_list) == len(fig.axes)

    return (
        fig,
        picks_list,
        titles_list,
        units_list,
        scalings_list,
        ax_list,
        make_label,
    )