<h3 dir="auto">Bug summary</h3>
<p dir="auto">I am trying to create a 3d histogram following the solution suggested <a href="https://stackoverflow.com/a/51624315/5472354" rel="nofollow">here</a>, which is based on [this matplotlib demo](Create 3D histogram of 2D data). In the finished plot however, the <code class="notranslate">zorder</code> of the bar sides are all messed up.</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
from matplotlib import cm


# source: https://stackoverflow.com/a/56923189/5472354
def gaus2d(x=0, y=0, mx=0, my=0, sx=1, sy=1):
    return 1. / (2. * np.pi * sx * sy) * np.exp(
        -((x - mx) ** 2. / (2. * sx ** 2.) + (y - my) ** 2. / (2. * sy ** 2.)))

sample_radius = 5
n_samples = 11
x = np.linspace(-sample_radius, sample_radius, num=n_samples)
y = np.linspace(-sample_radius, sample_radius, num=n_samples)
z = gaus2d(*np.meshgrid(x, y))


fig = plt.figure(constrained_layout=True)
ax = fig.add_subplot(111, projection='3d')
ax.axis('off')
ax.margins(0)

hist, xedges, yedges = np.histogram2d(x, y, bins=n_samples)
xpos, ypos = np.meshgrid(xedges[:-1] + xedges[1:], yedges[:-1] + yedges[1:])

xpos = xpos.flatten() / 2.
ypos = ypos.flatten() / 2.
zpos = np.zeros_like(xpos)

dx = xedges[1] - xedges[0]
dy = yedges[1] - yedges[0]
dz = z.flatten()

cmap = cm.get_cmap('viridis')
max_height = np.max(dz)
min_height = np.min(dz)

rgba = [cmap((k-min_height)/max_height) for k in dz]

ax.bar3d(
    xpos, ypos, zpos, dx, dy, dz, color=rgba, zsort='average', shade=True
)

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-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">cm</span>


<span class="pl-c"># source: https://stackoverflow.com/a/56923189/5472354</span>
<span class="pl-k">def</span> <span class="pl-en">gaus2d</span>(<span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">mx</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">my</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">sx</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">sy</span><span class="pl-c1">=</span><span class="pl-c1">1</span>):
    <span class="pl-k">return</span> <span class="pl-c1">1.</span> <span class="pl-c1">/</span> (<span class="pl-c1">2.</span> <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-s1">pi</span> <span class="pl-c1">*</span> <span class="pl-s1">sx</span> <span class="pl-c1">*</span> <span class="pl-s1">sy</span>) <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-en">exp</span>(
        <span class="pl-c1">-</span>((<span class="pl-s1">x</span> <span class="pl-c1">-</span> <span class="pl-s1">mx</span>) <span class="pl-c1">**</span> <span class="pl-c1">2.</span> <span class="pl-c1">/</span> (<span class="pl-c1">2.</span> <span class="pl-c1">*</span> <span class="pl-s1">sx</span> <span class="pl-c1">**</span> <span class="pl-c1">2.</span>) <span class="pl-c1">+</span> (<span class="pl-s1">y</span> <span class="pl-c1">-</span> <span class="pl-s1">my</span>) <span class="pl-c1">**</span> <span class="pl-c1">2.</span> <span class="pl-c1">/</span> (<span class="pl-c1">2.</span> <span class="pl-c1">*</span> <span class="pl-s1">sy</span> <span class="pl-c1">**</span> <span class="pl-c1">2.</span>)))

<span class="pl-s1">sample_radius</span> <span class="pl-c1">=</span> <span class="pl-c1">5</span>
<span class="pl-s1">n_samples</span> <span class="pl-c1">=</span> <span class="pl-c1">11</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">-</span><span class="pl-s1">sample_radius</span>, <span class="pl-s1">sample_radius</span>, <span class="pl-s1">num</span><span class="pl-c1">=</span><span class="pl-s1">n_samples</span>)
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">-</span><span class="pl-s1">sample_radius</span>, <span class="pl-s1">sample_radius</span>, <span class="pl-s1">num</span><span class="pl-c1">=</span><span class="pl-s1">n_samples</span>)
<span class="pl-s1">z</span> <span class="pl-c1">=</span> <span class="pl-en">gaus2d</span>(<span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-en">meshgrid</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</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">constrained_layout</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">111</span>, <span class="pl-s1">projection</span><span class="pl-c1">=</span><span class="pl-s">'3d'</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">axis</span>(<span class="pl-s">'off'</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">margins</span>(<span class="pl-c1">0</span>)

<span class="pl-s1">hist</span>, <span class="pl-s1">xedges</span>, <span class="pl-s1">yedges</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">histogram2d</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">bins</span><span class="pl-c1">=</span><span class="pl-s1">n_samples</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">meshgrid</span>(<span class="pl-s1">xedges</span>[:<span class="pl-c1">-</span><span class="pl-c1">1</span>] <span class="pl-c1">+</span> <span class="pl-s1">xedges</span>[<span class="pl-c1">1</span>:], <span class="pl-s1">yedges</span>[:<span class="pl-c1">-</span><span class="pl-c1">1</span>] <span class="pl-c1">+</span> <span class="pl-s1">yedges</span>[<span class="pl-c1">1</span>:])

<span class="pl-s1">xpos</span> <span class="pl-c1">=</span> <span class="pl-s1">xpos</span>.<span class="pl-en">flatten</span>() <span class="pl-c1">/</span> <span class="pl-c1">2.</span>
<span class="pl-s1">ypos</span> <span class="pl-c1">=</span> <span class="pl-s1">ypos</span>.<span class="pl-en">flatten</span>() <span class="pl-c1">/</span> <span class="pl-c1">2.</span>
<span class="pl-s1">zpos</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros_like</span>(<span class="pl-s1">xpos</span>)

<span class="pl-s1">dx</span> <span class="pl-c1">=</span> <span class="pl-s1">xedges</span>[<span class="pl-c1">1</span>] <span class="pl-c1">-</span> <span class="pl-s1">xedges</span>[<span class="pl-c1">0</span>]
<span class="pl-s1">dy</span> <span class="pl-c1">=</span> <span class="pl-s1">yedges</span>[<span class="pl-c1">1</span>] <span class="pl-c1">-</span> <span class="pl-s1">yedges</span>[<span class="pl-c1">0</span>]
<span class="pl-s1">dz</span> <span class="pl-c1">=</span> <span class="pl-s1">z</span>.<span class="pl-en">flatten</span>()

<span class="pl-s1">cmap</span> <span class="pl-c1">=</span> <span class="pl-s1">cm</span>.<span class="pl-en">get_cmap</span>(<span class="pl-s">'viridis'</span>)
<span class="pl-s1">max_height</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">max</span>(<span class="pl-s1">dz</span>)
<span class="pl-s1">min_height</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">min</span>(<span class="pl-s1">dz</span>)

<span class="pl-s1">rgba</span> <span class="pl-c1">=</span> [<span class="pl-en">cmap</span>((<span class="pl-s1">k</span><span class="pl-c1">-</span><span class="pl-s1">min_height</span>)<span class="pl-c1">/</span><span class="pl-s1">max_height</span>) <span class="pl-k">for</span> <span class="pl-s1">k</span> <span class="pl-c1">in</span> <span class="pl-s1">dz</span>]

<span class="pl-s1">ax</span>.<span class="pl-en">bar3d</span>(
    <span class="pl-s1">xpos</span>, <span class="pl-s1">ypos</span>, <span class="pl-s1">zpos</span>, <span class="pl-s1">dx</span>, <span class="pl-s1">dy</span>, <span class="pl-s1">dz</span>, <span class="pl-s1">color</span><span class="pl-c1">=</span><span class="pl-s1">rgba</span>, <span class="pl-s1">zsort</span><span class="pl-c1">=</span><span class="pl-s">'average'</span>, <span class="pl-s1">shade</span><span class="pl-c1">=</span><span class="pl-c1">True</span>
)

<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/48512334/227296083-722dd67c-f315-4d0b-8bff-275cef1ee043.png"><img src="https://user-images.githubusercontent.com/48512334/227296083-722dd67c-f315-4d0b-8bff-275cef1ee043.png" alt="grafik" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">A plot where the zorder is correct (although I have to admit, the result looks like a neat art piece).</p>
<h3 dir="auto">Additional information</h3>
<p dir="auto">I would guess it has something to do with the <code class="notranslate">zsort</code> argument, but I actually didn't manage to find the documentation for <code class="notranslate">bar3d</code>, only <a href="https://stackoverflow.com/a/47445406/5472354" rel="nofollow">this post</a>. I tried all three possibilities, but none of them worked. Is <code class="notranslate">bar3d</code> depreciated?</p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Windows 10</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.1</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">backend_interagg</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.9.7</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">conda</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong><br>
Z-order of rendered planes in 3d bar chart produced by bar3d is wrong. It is seen even in the example here <a href="https://matplotlib.org/gallery/mplot3d/3d_bars.html" rel="nofollow">https://matplotlib.org/gallery/mplot3d/3d_bars.html</a>. One of the bars (at location 2, 4, height 6) is drawn on top of the highest bar, even though it supposed to be behind it.</p>
<p dir="auto"><strong>Code for reproduction</strong><br>
A small modification of the code from example to make it more pronounced.</p>
<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
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import


# setup the figure and axes
fig = plt.figure(figsize=(8, 8))
ax1 = fig.add_subplot(111, projection='3d')

# fake data
_x = np.arange(8)
_y = np.arange(8)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()

top = x + y
bottom = np.zeros_like(top)
width = depth = 1

ax1.bar3d(x, y, bottom, width, depth, top, shade=True)
ax1.set_title('Shaded')
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-c"># This import registers the 3D projection, but is otherwise unused.</span>
<span class="pl-k">from</span> <span class="pl-s1">mpl_toolkits</span>.<span class="pl-s1">mplot3d</span> <span class="pl-k">import</span> <span class="pl-v">Axes3D</span>  <span class="pl-c"># noqa: F401 unused import</span>


<span class="pl-c"># setup the figure and axes</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">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">8</span>, <span class="pl-c1">8</span>))
<span class="pl-s1">ax1</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">111</span>, <span class="pl-s1">projection</span><span class="pl-c1">=</span><span class="pl-s">'3d'</span>)

<span class="pl-c"># fake data</span>
<span class="pl-s1">_x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">8</span>)
<span class="pl-s1">_y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">8</span>)
<span class="pl-s1">_xx</span>, <span class="pl-s1">_yy</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">meshgrid</span>(<span class="pl-s1">_x</span>, <span class="pl-s1">_y</span>)
<span class="pl-s1">x</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">_xx</span>.<span class="pl-en">ravel</span>(), <span class="pl-s1">_yy</span>.<span class="pl-en">ravel</span>()

<span class="pl-s1">top</span> <span class="pl-c1">=</span> <span class="pl-s1">x</span> <span class="pl-c1">+</span> <span class="pl-s1">y</span>
<span class="pl-s1">bottom</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros_like</span>(<span class="pl-s1">top</span>)
<span class="pl-s1">width</span> <span class="pl-c1">=</span> <span class="pl-s1">depth</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>

<span class="pl-s1">ax1</span>.<span class="pl-en">bar3d</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">bottom</span>, <span class="pl-s1">width</span>, <span class="pl-s1">depth</span>, <span class="pl-s1">top</span>, <span class="pl-s1">shade</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">ax1</span>.<span class="pl-en">set_title</span>(<span class="pl-s">'Shaded'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9054941/54744129-1905ce80-4c01-11e9-808a-39a54d870363.png"><img src="https://user-images.githubusercontent.com/9054941/54744129-1905ce80-4c01-11e9-808a-39a54d870363.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">Everything to the right from diagonal is rendered incorrectly.</p>
<p dir="auto"><strong>Matplotlib version</strong><br>
Tested on two machines with the same results.</p>
<ul dir="auto">
<li>Operating system: mac and windows</li>
<li>Matplotlib version:<br>
matplotlib                2.1.2            py36h6d6146d_0<br>
and<br>
matplotlib                3.0.3            py37_0    conda-forge</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>): module://ipykernel.pylab.backend_inline</li>
<li>Python version:<br>
3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 13:19:00)<br>
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]<br>
and<br>
3.7.1 | packaged by conda-forge | (default, Mar 13 2019, 13:32:59) [MSC v.1900 64 bit (AMD64)]</li>
<li>Jupyter version (if applicable):<br>
ipython                   5.1.0                    py36_1<br>
and<br>
ipython                   7.3.0            py37h39e3cac_0    conda-forge</li>
</ul>