<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong><br>
Playback of mp4 files written by FFMpegWriter shows a rolling image if the dpi setting does not match that specified when a figure is created. In particular, this happens if the movie frame images are being displayed on the screen, e.g. during creation of the movie. (There is also an annoying flicker.)</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<p dir="auto">Animation in the code below fails, resulting in a rolling image on playback of the saved mp4 movie.  Either of two modifications indicated in the comments make the saved movie play back normally.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.animation
import numpy
import pylab

fig = pylab.figure(figsize=(6,6), dpi=120)
writer = matplotlib.animation.FFMpegWriter(fps=10)
x = numpy.linspace(0, 6.28, 100)

with writer.saving(fig, 'rolling_bug.mp4', dpi=100):           # saving OK if dpi=120
    for offset in numpy.linspace(0, 6.28, 30):
        pylab.plot(numpy.cos(x-offset), color='black')
        # saving OK if the next line is commented out
        pylab.pause(0.01)
        writer.grab_frame()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">animation</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span>
<span class="pl-k">import</span> <span class="pl-s1">pylab</span>

<span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">pylab</span>.<span class="pl-en">figure</span>(<span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>,<span class="pl-c1">6</span>), <span class="pl-s1">dpi</span><span class="pl-c1">=</span><span class="pl-c1">120</span>)
<span class="pl-s1">writer</span> <span class="pl-c1">=</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">animation</span>.<span class="pl-v">FFMpegWriter</span>(<span class="pl-s1">fps</span><span class="pl-c1">=</span><span class="pl-c1">10</span>)
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">6.28</span>, <span class="pl-c1">100</span>)

<span class="pl-k">with</span> <span class="pl-s1">writer</span>.<span class="pl-en">saving</span>(<span class="pl-s1">fig</span>, <span class="pl-s">'rolling_bug.mp4'</span>, <span class="pl-s1">dpi</span><span class="pl-c1">=</span><span class="pl-c1">100</span>):           <span class="pl-c"># saving OK if dpi=120</span>
    <span class="pl-k">for</span> <span class="pl-s1">offset</span> <span class="pl-c1">in</span> <span class="pl-s1">numpy</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">6.28</span>, <span class="pl-c1">30</span>):
        <span class="pl-s1">pylab</span>.<span class="pl-en">plot</span>(<span class="pl-s1">numpy</span>.<span class="pl-en">cos</span>(<span class="pl-s1">x</span><span class="pl-c1">-</span><span class="pl-s1">offset</span>), <span class="pl-s1">color</span><span class="pl-c1">=</span><span class="pl-s">'black'</span>)
        <span class="pl-c"># saving OK if the next line is commented out</span>
        <span class="pl-s1">pylab</span>.<span class="pl-en">pause</span>(<span class="pl-c1">0.01</span>)
        <span class="pl-s1">writer</span>.<span class="pl-en">grab_frame</span>()</pre></div>
<p dir="auto">Note that commenting out the pylab.pause() call results in no image being displayed on the screen until the end of the script.</p>
<p dir="auto"><strong>Actual outcome</strong><br>
Inspection of the figure vertical dimension in pixels shows that it has changed across the call to grab_frame(), from 600 to 590 in the above example.  The horizontal dimension does not change.<br>
A screen grab from VLC Media Player is shown here:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7269332/27990577-278e5b28-6410-11e7-8538-123c0c394891.png"><img src="https://user-images.githubusercontent.com/7269332/27990577-278e5b28-6410-11e7-8538-123c0c394891.png" alt="rolling_bug_screengrab" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">The writer.grab_frame() should not change the vertical dimension of the figure.</p>
<p dir="auto">The flickering did not occur in the last version of Matplotlib I had installed, but I do not know what version that was.  I'm pretty sure the vertical image dimension also stayed constant across the grab_frame() call, since this was (abstracted from) working code from about a year ago.</p>
<p dir="auto"><strong>Version info</strong></p>
<p dir="auto">Windows 8.1<br>
Matplotlib 2.0.2<br>
Python 3.6.1<br>
Anaconda 4.4.0</p>
<p dir="auto">Installed from conda</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">The <code class="notranslate">print_figure</code> function in the <code class="notranslate">FigureCanvasBase</code> class records the current figure's dpi, sets it to the requested dpi for the saved figure, and then resets it to the original. Setting the dpi invokes a property function, <code class="notranslate">_set_dpi</code> that calls the figure's <code class="notranslate">set_size_inches</code> method, which then triggers a resize of the figure manager.</p>
<p dir="auto">This resize is not evident when I use the standard Pyplot commands within a Qt console, presumably because the plot size doesn't change. However, I have embedded the Matplotlib figure in a PyQt GUI where I don't have the toolbar in the same widget, and it causes a noticeable size change for a fraction of a second before the second dpi reset restores the old size.</p>
<p dir="auto"><strong>Possible solution</strong></p>
<p dir="auto">I would like to suggest that the <code class="notranslate">FigureCanvasBase</code> <code class="notranslate">print_figure</code> function should not invoke the dpi property, but set and then reset the figure's hidden variable, <code class="notranslate">_dpi</code>, which will not trigger the redundant canvas resize. I can submit a pull request, but I wanted to check if there would be side effects.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Matplotlib Version: 2.0.2</li>
<li>Operating System: Mac OS 10.12.5</li>
<li>Python Version: 2.7.11</li>
<li>PyQt 4.8.7</li>
</ul>