<p dir="auto">It would be great if we can have either a <code class="notranslate">jitter</code> command or a <code class="notranslate">jitter</code> flag in <code class="notranslate">scatter</code>.<br>
I'm refering to something like <a href="http://docs.ggplot2.org/current/geom_jitter.html" rel="nofollow">ggplot2's geom_jitter</a>.</p>
<p dir="auto">A basic solution:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def rand_jitter(arr):
    stdev = arr.max()/100.
    return arr + np.random.randn(len(arr)) * stdev

def jitter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs):
    return scatter(rand_jitter(x), rand_jitter(y), s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs)"><pre class="notranslate"><code class="notranslate">def rand_jitter(arr):
    stdev = arr.max()/100.
    return arr + np.random.randn(len(arr)) * stdev

def jitter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs):
    return scatter(rand_jitter(x), rand_jitter(y), s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs)
</code></pre></div>
<p dir="auto">Of course the definition of <code class="notranslate">stdev</code> needs to be better, this will only work for some cases.</p>
<p dir="auto">The idea is from <a href="http://stackoverflow.com/questions/8671808/matplotlib-preventing-overlaying-datapoints" rel="nofollow">this SoF post</a>.</p>
<h1 dir="auto">Bug report</h1>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">Changing the figure's dpi via <code class="notranslate">fig.set_dpi()</code> does not have the desired effect. There are strange things happening when saving or showing the figure as detailed below.</p>
<h3 dir="auto">Bug 1 - saving</h3>
<p dir="auto">In the following a figure with dpi=200 should be saved in all 3 cases.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt

get_img_size = lambda path: print(plt.imread(path).shape)

fig1, ax1 = plt.subplots(dpi=200)
ax1.plot([1,2])
fig1.savefig(&quot;fig1.png&quot;)
get_img_size(&quot;fig1.png&quot;)

fig2, ax2 = plt.subplots(dpi=100)
ax2.plot([1,2])
fig2.savefig(&quot;fig2.png&quot;, dpi=200)
get_img_size(&quot;fig2.png&quot;)

fig3, ax3 = plt.subplots(dpi=100)
ax3.plot([1,2])
fig3.set_dpi(200)
fig3.savefig(&quot;fig3.png&quot;, dpi=&quot;figure&quot;)
get_img_size(&quot;fig3.png&quot;)"><pre class="notranslate"><code class="notranslate">import matplotlib.pyplot as plt

get_img_size = lambda path: print(plt.imread(path).shape)

fig1, ax1 = plt.subplots(dpi=200)
ax1.plot([1,2])
fig1.savefig("fig1.png")
get_img_size("fig1.png")

fig2, ax2 = plt.subplots(dpi=100)
ax2.plot([1,2])
fig2.savefig("fig2.png", dpi=200)
get_img_size("fig2.png")

fig3, ax3 = plt.subplots(dpi=100)
ax3.plot([1,2])
fig3.set_dpi(200)
fig3.savefig("fig3.png", dpi="figure")
get_img_size("fig3.png")
</code></pre></div>
<p dir="auto"><strong>Qt5Agg</strong></p>
<p dir="auto">The outcome using the Qt5Agg backend of this is</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(960, 1280, 4)
(960, 1280, 4)
(480, 640, 4)"><pre class="notranslate"><code class="notranslate">(960, 1280, 4)
(960, 1280, 4)
(480, 640, 4)
</code></pre></div>
<p dir="auto">i.e. in the first two cases we get the expected 200 dpi figure. In the last case, where the figure dpi is set via <strong><code class="notranslate">fig.set_dpi</code> it seems to be ignored</strong> and the output image has a dpi of 100.</p>
<p dir="auto"><strong>TkAgg</strong></p>
<p dir="auto">Using the TkAgg backend, the saved figures are as expected (<code class="notranslate">(960, 1280, 4)</code> in all cases.)</p>
<h3 dir="auto">Bug 2 - showing</h3>
<p dir="auto">In the following we would expect to get two identical figures shown, both with dpi=100 and 640x480 pixels large.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="plt.close(&quot;all&quot;)

fig1, ax1 = plt.subplots(num=&quot;expected&quot;, dpi=100)
ax1.plot([1,2])

fig3, ax3 = plt.subplots(num=&quot;buggy&quot;, dpi=50)
ax3.plot([1,2])
fig3.set_dpi(100)
print(fig3.get_dpi())
fig3.savefig(&quot;fig3a.png&quot;, dpi=&quot;figure&quot;)
print(fig3.get_dpi())
plt.show()
print(fig3.get_dpi())"><pre class="notranslate"><code class="notranslate">plt.close("all")

fig1, ax1 = plt.subplots(num="expected", dpi=100)
ax1.plot([1,2])

fig3, ax3 = plt.subplots(num="buggy", dpi=50)
ax3.plot([1,2])
fig3.set_dpi(100)
print(fig3.get_dpi())
fig3.savefig("fig3a.png", dpi="figure")
print(fig3.get_dpi())
plt.show()
print(fig3.get_dpi())
</code></pre></div>
<p dir="auto"><strong>Qt5Agg</strong></p>
<p dir="auto">The output (with Qt5Agg) is the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="100
100
50"><pre class="notranslate"><code class="notranslate">100
100
50
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/23121882/39899083-43d2f03c-54ba-11e8-9642-54aa1b243134.png"><img src="https://user-images.githubusercontent.com/23121882/39899083-43d2f03c-54ba-11e8-9642-54aa1b243134.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">The printed dpi is correctly 100 before showing the figure. After showing, it prints 50. The shown figures have both the correct size, but the figure where dpi is set via <code class="notranslate">fig.set_dpi</code> has narrower linewidth and smaller fontsize. It looks exaclty like a figure that would have been expected for dpi=50, just with the wrong pixel size.</p>
<p dir="auto"><strong>TkAgg</strong></p>
<p dir="auto">Using TkAgg backend, the output is correctly printed as <code class="notranslate">100</code> in all cases, <strong>but</strong> the shown figures differ in size; the left (expected) figure is 640x480 pixels, while the right figure is 640x445 pixels.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/23121882/39899772-1f1abe10-54be-11e8-99cb-f09df028fd44.png"><img src="https://user-images.githubusercontent.com/23121882/39899772-1f1abe10-54be-11e8-99cb-f09df028fd44.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Windows 8.1</li>
<li>Matplotlib version: master (2.2.2.post1088.dev0+g9ec4b95d6)</li>
<li>Matplotlib backend: Qt5Agg &amp; TkAgg (see text)</li>
<li>Python version: 3.6.4</li>
</ul>
<hr>
<p dir="auto">Running the same with matplotlib 2.0.2 (all other versions the same) I get<br>
Qt5Agg, saving: <code class="notranslate">(960, 1280, 4), (960, 1280, 4), (480, 640, 4)</code>  - same bug as above with master.<br>
Qt5Agg, showing <code class="notranslate">100, 100, 100</code> - printed dpi are all the same, <strong>shown figures are the same</strong>.<br>
TkAgg, saving: <code class="notranslate">(960, 1280, 4)</code> in all cases. - same expected behaviour as above.<br>
TkAgg, showing <code class="notranslate">100, 100, 100</code> - printed dpi are all the same, shown figures differ in same manner as above.</p>