matplotlib / matplotlib

matplotlib: plotting with Python
https://matplotlib.org/stable/
20.28k stars 7.65k forks source link

[Bug]: incorrect display of 3d barchart #25333

Closed 51moon closed 1 year ago

51moon commented 1 year ago

Bug summary

There are several rendering errors when creating a bar3d chart:

The list of errors is not complete, I think everyone can recognize the errors when looking closely.

Code for reproduction

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# generate data
x = np.array(10*list(range(10)))
y = np.array([])
for i in x[0:10]:
    y = np.append(y,np.ones(10)*i)
rng = np.random.default_rng(4)
z = rng.random(len(x))+0.5

# plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
x_pos = x-1/2
y_pos = y-1/2
z_pos = [0]*len(z)
x_size = np.ones(len(z))
y_size = np.ones(len(z))
z_size = z
colormap = cm.ScalarMappable()
color = colormap.to_rgba(z)
ax.bar3d(x_pos, y_pos, z_pos, x_size, y_size, z_size, color=color, shade=True)
plt.savefig('plt.png', dpi=300)

Actual outcome

plt

Expected outcome

I expect a correct 3D rendering

Additional information

There is a similar bug reported here.

Operating system

Linux Mint

Matplotlib Version

3.7.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.10.6

Jupyter version

No response

Installation

pip

jklymak commented 1 year ago

This is basically expected. Matplotlib doesn't have a full 3D rendering pipeline so individual artists are drawn one at a time with no knowledge of the other artists. When you have many of them together like this, their zorders overlap and things don't look right.

It would be great if Matplotlib had a proper 3D engine but that is unlikely to happen any time soon, and previous attempts have simply been spun out to other projects.

51moon commented 1 year ago

Ok, that's a pity, but good to know. So I will look around for an alternative. Can you name some of these spin-off projects?

jklymak commented 1 year ago

mayavi is the one that comes to my mind, but there are others.

jklymak commented 1 year ago

You may also want to check out the experimental: https://github.com/rougier/matplotlib-3d

anntzer commented 1 year ago

Closing as essentially duplicate of #13728.