<h3 dir="auto">Bug summary</h3>
<p dir="auto">Pixel widths appear irregularly in <code class="notranslate">pcolor(x, y, z, rasterized=True)</code>, while <code class="notranslate">x</code> and <code class="notranslate">y</code> are regular. Using <code class="notranslate">rasterized=False</code> provides correct (equal size) pixels, but saving the figure to <code class="notranslate">.png</code> (instead of <code class="notranslate">.pdf</code>) will show the same irregular pixel width bug.</p>
<p dir="auto">It looks like a resampling bug, which messes up precise data visualization completely.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib.pyplot as plt
x, y = np.meshgrid(range(500), range(2))
z = [np.tile([0,1],250), np.tile([0,1],250)]
fig, ax = plt.subplots(1,1, figsize=(10,2))
im = ax.pcolor(x, y, z, rasterized=True)
ax.set_xticks(np.arange(0,40))
fig.savefig('test.pdf')
plt.show()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-s1">x</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">meshgrid</span>(<span class="pl-en">range</span>(<span class="pl-c1">500</span>), <span class="pl-en">range</span>(<span class="pl-c1">2</span>))
<span class="pl-s1">z</span> <span class="pl-c1">=</span> [<span class="pl-s1">np</span>.<span class="pl-en">tile</span>([<span class="pl-c1">0</span>,<span class="pl-c1">1</span>],<span class="pl-c1">250</span>), <span class="pl-s1">np</span>.<span class="pl-en">tile</span>([<span class="pl-c1">0</span>,<span class="pl-c1">1</span>],<span class="pl-c1">250</span>)]
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-c1">1</span>,<span class="pl-c1">1</span>, <span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">10</span>,<span class="pl-c1">2</span>))
<span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">pcolor</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">z</span>, <span class="pl-s1">rasterized</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">set_xticks</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-c1">40</span>))
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test.pdf'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto">In the resulting plot, some pixels are thinner, and some are thicker, than they should be.<br>
<code class="notranslate">rasterize=True</code> (zoom-in to the lower left corner)<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7942719/177547093-64eb91af-1345-4be1-a04c-e99bd7ccfd49.png"><img src="https://user-images.githubusercontent.com/7942719/177547093-64eb91af-1345-4be1-a04c-e99bd7ccfd49.png" alt="grafik" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto"><code class="notranslate">rasterize=False</code> (zoom-in to the lower left corner)<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7942719/177547007-82796b90-1863-4370-8718-0a1c744b3119.png"><img src="https://user-images.githubusercontent.com/7942719/177547007-82796b90-1863-4370-8718-0a1c744b3119.png" alt="grafik" style="max-width: 100%;"></a></p>
<h3 dir="auto">Additional information</h3>
<ul dir="auto">
<li>This behaviour depends on the number of pixels plotted, for me it becomes visible above <code class="notranslate">len(x)&gt;100</code>.</li>
<li>The spread between thicker and thinner lines increases with increasing size of the input array.</li>
<li>If the view is reduced to a lower number of pixels (e.g., <code class="notranslate">ax.set_xlim(0,10)</code>), the effect is no longer visible.</li>
</ul>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Windows</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.5.1</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">inline</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.9.6</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>
<p dir="auto">Matplotlib version 1.5.1<br>
Python 3.4.4 on Windows via Anaconda 2.2.0 (64-bit)<br>
Also occured on a Python 3.5.1 install on Lubuntu via Anaconda 4.0<br>
Jupyter QtConsole 4.1.0; IPython verion 4.0.0</p>
<p dir="auto">I made some functions for positioning and formatting x and y labels and to draw arrows pointing towards these labels.  Today I wrote a function that similarly formats the colorbar label.  However, the behavior is erroneous if the function is called within a block of code or in a script executed on the command line. I defined a condensed version of the function here as <code class="notranslate">mycolorbar()</code>.</p>
<p dir="auto">If I run the following in a script from the command line, or if I paste everything into a single code block in the Jupyter QtConsole, I get the figure shown below the code.  In the QtConsole this occurs regardless of whether I've called <code class="notranslate">%matplotlib</code></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt
import numpy as np

def mycolorbar(ax,cbar):
    fig = ax.get_figure()
    cax = cbar.ax
    inv = ax.transAxes.inverted()
    R = fig.canvas.get_renderer()
    caxY = cax.yaxis
    ylab = caxY.get_label()
    ticklabelbbox = inv.transform( caxY.get_ticklabel_extents(R)[1].get_points() )
    ylab.set_verticalalignment('center')
    ylab.set_horizontalalignment('center')
    ylab.set_rotation(0)
    xpos, ypos = np.max(ticklabelbbox[:,0]), (3/4)
    caxY.set_label_coords( xpos , ypos , transform = ax.transAxes )

x,y,z = [np.random.rand(1000) for i in range(3)]

fig = plt.figure(facecolor='w')
ax = fig.add_axes([.1,.1,.8,.8])
plt.tricontourf(x,y,z)
plt.xlabel('xlab')
plt.ylabel('ylab')
cbar = plt.colorbar(label='clab')
mycolorbar( ax, cbar)
plt.show()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-k">def</span> <span class="pl-en">mycolorbar</span>(<span class="pl-s1">ax</span>,<span class="pl-s1">cbar</span>):
    <span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">get_figure</span>()
    <span class="pl-s1">cax</span> <span class="pl-c1">=</span> <span class="pl-s1">cbar</span>.<span class="pl-s1">ax</span>
    <span class="pl-s1">inv</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-s1">transAxes</span>.<span class="pl-en">inverted</span>()
    <span class="pl-v">R</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-s1">canvas</span>.<span class="pl-en">get_renderer</span>()
    <span class="pl-s1">caxY</span> <span class="pl-c1">=</span> <span class="pl-s1">cax</span>.<span class="pl-s1">yaxis</span>
    <span class="pl-s1">ylab</span> <span class="pl-c1">=</span> <span class="pl-s1">caxY</span>.<span class="pl-en">get_label</span>()
    <span class="pl-s1">ticklabelbbox</span> <span class="pl-c1">=</span> <span class="pl-s1">inv</span>.<span class="pl-en">transform</span>( <span class="pl-s1">caxY</span>.<span class="pl-en">get_ticklabel_extents</span>(<span class="pl-v">R</span>)[<span class="pl-c1">1</span>].<span class="pl-en">get_points</span>() )
    <span class="pl-s1">ylab</span>.<span class="pl-en">set_verticalalignment</span>(<span class="pl-s">'center'</span>)
    <span class="pl-s1">ylab</span>.<span class="pl-en">set_horizontalalignment</span>(<span class="pl-s">'center'</span>)
    <span class="pl-s1">ylab</span>.<span class="pl-en">set_rotation</span>(<span class="pl-c1">0</span>)
    <span class="pl-s1">xpos</span>, <span class="pl-s1">ypos</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">max</span>(<span class="pl-s1">ticklabelbbox</span>[:,<span class="pl-c1">0</span>]), (<span class="pl-c1">3</span><span class="pl-c1">/</span><span class="pl-c1">4</span>)
    <span class="pl-s1">caxY</span>.<span class="pl-en">set_label_coords</span>( <span class="pl-s1">xpos</span> , <span class="pl-s1">ypos</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">x</span>,<span class="pl-s1">y</span>,<span class="pl-s1">z</span> <span class="pl-c1">=</span> [<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-c1">1000</span>) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">3</span>)]

<span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>(<span class="pl-s1">facecolor</span><span class="pl-c1">=</span><span class="pl-s">'w'</span>)
<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_axes</span>([<span class="pl-c1">.1</span>,<span class="pl-c1">.1</span>,<span class="pl-c1">.8</span>,<span class="pl-c1">.8</span>])
<span class="pl-s1">plt</span>.<span class="pl-en">tricontourf</span>(<span class="pl-s1">x</span>,<span class="pl-s1">y</span>,<span class="pl-s1">z</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">xlabel</span>(<span class="pl-s">'xlab'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">ylabel</span>(<span class="pl-s">'ylab'</span>)
<span class="pl-s1">cbar</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">label</span><span class="pl-c1">=</span><span class="pl-s">'clab'</span>)
<span class="pl-en">mycolorbar</span>( <span class="pl-s1">ax</span>, <span class="pl-s1">cbar</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/13245062/15520253/f8b4b942-21ca-11e6-9bd7-a9b67a84631c.png"><img src="https://cloud.githubusercontent.com/assets/13245062/15520253/f8b4b942-21ca-11e6-9bd7-a9b67a84631c.png" alt="farout" style="max-width: 100%;"></a></p>
<p dir="auto">Observe how far out to the right the colorbar label are.  This is "incorrect".</p>
<p dir="auto">Now, when working interactively in the QtConsole, if I run all of the above code <strong>_except</strong>_ the last line, let the figure window render, then execute that last line <code class="notranslate">mycolorbar( ax, cbar)</code> on its own, I get the "correct" position for the colorbar label.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/13245062/15520314/418140a0-21cb-11e6-9812-79d6dbd1c990.png"><img src="https://cloud.githubusercontent.com/assets/13245062/15520314/418140a0-21cb-11e6-9812-79d6dbd1c990.png" alt="closein" style="max-width: 100%;"></a></p>
<p dir="auto">I'm asking anyone to debug my code.  Just trying to bring a potential issue to light.  Thank you.</p>