<p dir="auto">Pickling <code class="notranslate">imshow</code> objects (I'm using them used for heatmaps) fails with the following error in IPython after <code class="notranslate">%matplotlib inline</code> magic has been used. If inline figures are not set they work as expected.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="UnpickleableError: Cannot pickle &lt;type 'Image'&gt; objects"><pre class="notranslate"><code class="notranslate">UnpickleableError: Cannot pickle &lt;type 'Image'&gt; objects
</code></pre></div>
<p dir="auto">Using <code class="notranslate">matplotlib.tests.test_pickle.recursive_pickle</code> fails reporting the following (similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="10443960" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/1719" data-hovercard-type="issue" data-hovercard-url="/matplotlib/matplotlib/issues/1719/hovercard" href="https://github.com/matplotlib/matplotlib/issues/1719">#1719</a>):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Failed to pickle attribute &quot;gridline&quot; in (list/tuple item #0 in (attribute &quot;majorTicks&quot; in (attribute &quot;xaxis&quot; in (list/tuple item #1 in (list/tuple item #1 in (list/tuple item #0 in (attribute &quot;_elements&quot; in (attribute &quot;_axstack&quot; in (top level object))))))))).  Type: &lt;class 'matplotlib.lines.Line2D'&gt;"><pre class="notranslate"><code class="notranslate">Failed to pickle attribute "gridline" in (list/tuple item #0 in (attribute "majorTicks" in (attribute "xaxis" in (list/tuple item #1 in (list/tuple item #1 in (list/tuple item #0 in (attribute "_elements" in (attribute "_axstack" in (top level object))))))))).  Type: &lt;class 'matplotlib.lines.Line2D'&gt;
</code></pre></div>
<p dir="auto">However, using <code class="notranslate">matplotlib.tests.test_pickle.depth_getter</code> its possible to identify the object that is the cause of the initial error. It's at:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="140628613187224 (8, &lt;Image object at 0x7fe6a688c698&gt;, u'attribute &quot;_imcache&quot; in (list/tuple item #0 in (attribute &quot;images&quot; in (list/tuple item #1 in (list/tuple item #1 in (list/tuple item #0 in (attribute &quot;_elements&quot; in (attribute &quot;_axstack&quot; in (top level object))))))))')"><pre class="notranslate"><code class="notranslate">140628613187224 (8, &lt;Image object at 0x7fe6a688c698&gt;, u'attribute "_imcache" in (list/tuple item #0 in (attribute "images" in (list/tuple item #1 in (list/tuple item #1 in (list/tuple item #0 in (attribute "_elements" in (attribute "_axstack" in (top level object))))))))')
</code></pre></div>
<p dir="auto">It looks as though this is a cache of the rendered image. Can these be ignored when pickling?</p>
<p dir="auto">Couple of issues I've found.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def latex_float(f, pos=0):
    float_str = &quot;{0:.2g}&quot;.format(f)
    if &quot;e&quot; in float_str:
        base, exponent = float_str.split(&quot;e&quot;)
        return r&quot;${0} \times 10^{{{1}}}$&quot;.format(base, int(exponent))
    else:
        return r&quot;${}$&quot;.format(float_str)

formatter = FuncFormatter(latex_float)

corr = np.corrcoef(data['x'], data['y'])[0,1]
fig, ax = pl.subplots(figsize=figsize)
hist, _, _, im = ax.hist2d(data['x'], data['y'], norm=LogNorm(), bins=(bins_x, bins_y) , alpha=0.75, cmap = cmap)

cbar = fig.colorbar(im, ticks=[0., 1., 2., 5., 10., 20., 50., 100., 200., 500., 1000., 2000., 5000.], format=formatter)
cbar.set_label('number density', fontsize=labelsize, labelpad=79)
cbar.ax.tick_params(labelsize=labelsize-6, width=3, length=15)
cbar.ax.minorticks_on()

ax.set_xlabel(label_x, fontsize=labelsize)
ax.set_ylabel(label_y, fontsize=labelsize)
ax.grid(True, which='both', linewidth=3, linestyle='--', alpha=0.5)
ax.text(0.95, 0.05, '$\mathrm{{Corr}} = {:0.4f}$'.format(corr), transform=ax.transAxes, fontsize=labelsize, verticalalignment='bottom', horizontalalignment='right', bbox=textprops)

ax.tick_params(axis='both', which='major', labelsize=labelsize-6)

pickle.dump(ax, file('ZH_ax.pkl', 'w+'))

fig.close()"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">latex_float</span>(<span class="pl-s1">f</span>, <span class="pl-s1">pos</span><span class="pl-c1">=</span><span class="pl-c1">0</span>):
    <span class="pl-s1">float_str</span> <span class="pl-c1">=</span> <span class="pl-s">"{0:.2g}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">f</span>)
    <span class="pl-k">if</span> <span class="pl-s">"e"</span> <span class="pl-c1">in</span> <span class="pl-s1">float_str</span>:
        <span class="pl-s1">base</span>, <span class="pl-s1">exponent</span> <span class="pl-c1">=</span> <span class="pl-s1">float_str</span>.<span class="pl-en">split</span>(<span class="pl-s">"e"</span>)
        <span class="pl-k">return</span> <span class="pl-s">r"${0} \times 10^{{{1}}}$"</span>.<span class="pl-en">format</span>(<span class="pl-s1">base</span>, <span class="pl-en">int</span>(<span class="pl-s1">exponent</span>))
    <span class="pl-k">else</span>:
        <span class="pl-k">return</span> <span class="pl-s">r"${}$"</span>.<span class="pl-en">format</span>(<span class="pl-s1">float_str</span>)

<span class="pl-s1">formatter</span> <span class="pl-c1">=</span> <span class="pl-v">FuncFormatter</span>(<span class="pl-s1">latex_float</span>)

<span class="pl-s1">corr</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">corrcoef</span>(<span class="pl-s1">data</span>[<span class="pl-s">'x'</span>], <span class="pl-s1">data</span>[<span class="pl-s">'y'</span>])[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>]
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">pl</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">figsize</span><span class="pl-c1">=</span><span class="pl-s1">figsize</span>)
<span class="pl-s1">hist</span>, <span class="pl-s1">_</span>, <span class="pl-s1">_</span>, <span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">hist2d</span>(<span class="pl-s1">data</span>[<span class="pl-s">'x'</span>], <span class="pl-s1">data</span>[<span class="pl-s">'y'</span>], <span class="pl-s1">norm</span><span class="pl-c1">=</span><span class="pl-v">LogNorm</span>(), <span class="pl-s1">bins</span><span class="pl-c1">=</span>(<span class="pl-s1">bins_x</span>, <span class="pl-s1">bins_y</span>) , <span class="pl-s1">alpha</span><span class="pl-c1">=</span><span class="pl-c1">0.75</span>, <span class="pl-s1">cmap</span> <span class="pl-c1">=</span> <span class="pl-s1">cmap</span>)

<span class="pl-s1">cbar</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">im</span>, <span class="pl-s1">ticks</span><span class="pl-c1">=</span>[<span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">2.</span>, <span class="pl-c1">5.</span>, <span class="pl-c1">10.</span>, <span class="pl-c1">20.</span>, <span class="pl-c1">50.</span>, <span class="pl-c1">100.</span>, <span class="pl-c1">200.</span>, <span class="pl-c1">500.</span>, <span class="pl-c1">1000.</span>, <span class="pl-c1">2000.</span>, <span class="pl-c1">5000.</span>], <span class="pl-s1">format</span><span class="pl-c1">=</span><span class="pl-s1">formatter</span>)
<span class="pl-s1">cbar</span>.<span class="pl-en">set_label</span>(<span class="pl-s">'number density'</span>, <span class="pl-s1">fontsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span>, <span class="pl-s1">labelpad</span><span class="pl-c1">=</span><span class="pl-c1">79</span>)
<span class="pl-s1">cbar</span>.<span class="pl-s1">ax</span>.<span class="pl-en">tick_params</span>(<span class="pl-s1">labelsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span><span class="pl-c1">-</span><span class="pl-c1">6</span>, <span class="pl-s1">width</span><span class="pl-c1">=</span><span class="pl-c1">3</span>, <span class="pl-s1">length</span><span class="pl-c1">=</span><span class="pl-c1">15</span>)
<span class="pl-s1">cbar</span>.<span class="pl-s1">ax</span>.<span class="pl-en">minorticks_on</span>()

<span class="pl-s1">ax</span>.<span class="pl-en">set_xlabel</span>(<span class="pl-s1">label_x</span>, <span class="pl-s1">fontsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">set_ylabel</span>(<span class="pl-s1">label_y</span>, <span class="pl-s1">fontsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">grid</span>(<span class="pl-c1">True</span>, <span class="pl-s1">which</span><span class="pl-c1">=</span><span class="pl-s">'both'</span>, <span class="pl-s1">linewidth</span><span class="pl-c1">=</span><span class="pl-c1">3</span>, <span class="pl-s1">linestyle</span><span class="pl-c1">=</span><span class="pl-s">'--'</span>, <span class="pl-s1">alpha</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">text</span>(<span class="pl-c1">0.95</span>, <span class="pl-c1">0.05</span>, <span class="pl-s">'$\mathrm{{Corr}} = {:0.4f}$'</span>.<span class="pl-en">format</span>(<span class="pl-s1">corr</span>), <span class="pl-s1">transform</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>.<span class="pl-s1">transAxes</span>, <span class="pl-s1">fontsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span>, <span class="pl-s1">verticalalignment</span><span class="pl-c1">=</span><span class="pl-s">'bottom'</span>, <span class="pl-s1">horizontalalignment</span><span class="pl-c1">=</span><span class="pl-s">'right'</span>, <span class="pl-s1">bbox</span><span class="pl-c1">=</span><span class="pl-s1">textprops</span>)

<span class="pl-s1">ax</span>.<span class="pl-en">tick_params</span>(<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s">'both'</span>, <span class="pl-s1">which</span><span class="pl-c1">=</span><span class="pl-s">'major'</span>, <span class="pl-s1">labelsize</span><span class="pl-c1">=</span><span class="pl-s1">labelsize</span><span class="pl-c1">-</span><span class="pl-c1">6</span>)

<span class="pl-s1">pickle</span>.<span class="pl-en">dump</span>(<span class="pl-s1">ax</span>, <span class="pl-en">file</span>(<span class="pl-s">'ZH_ax.pkl'</span>, <span class="pl-s">'w+'</span>))

<span class="pl-s1">fig</span>.<span class="pl-en">close</span>()</pre></div>
<p dir="auto">As it is, the code gives the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;make_plots_new.py&quot;, line 124, in &lt;module&gt;
    pickle.dump(fig, file('ZH_ax.pkl', 'w+'))
  File &quot;/lib/python2.7/copy_reg.py&quot;, line 70, in _reduce_ex
    raise TypeError, &quot;can't pickle %s objects&quot; % base.__name__
TypeError: can't pickle instancemethod objects"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "make_plots_new.py", line 124, in &lt;module&gt;
    pickle.dump(fig, file('ZH_ax.pkl', 'w+'))
  File "/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects
</code></pre></div>
<p dir="auto">If I exclude the label formatter function in the <code class="notranslate">colorbar</code>, I get an error of</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;make_plots_new.py&quot;, line 124, in &lt;module&gt;
    pickle.dump(fig, file('ZH_ax.pkl', 'w+'))
cPickle.UnpickleableError: Cannot pickle &lt;type 'Image'&gt; objects"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "make_plots_new.py", line 124, in &lt;module&gt;
    pickle.dump(fig, file('ZH_ax.pkl', 'w+'))
cPickle.UnpickleableError: Cannot pickle &lt;type 'Image'&gt; objects
</code></pre></div>
<p dir="auto">How can one pickle?</p>