-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Incorrect overlap of markers in scatter3D #5830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
attn @WeatherGod It looks like the zsort logic from |
Indeed. I think this has been noticed before in the theoretical sense, but On Mon, Jan 11, 2016 at 7:54 AM, Thomas A Caswell [email protected]
|
Just a minor addition. I've run into what I assume is the same bug. But I tracked down the particular elevation at which the problem occurs for me: 48.02450 degrees. Maybe this is just down to the peculiar particulars of precisely my problem, but maybe you could find it an interesting test or helpful in some other way. I've gisted the example here. |
I've run into this with the z-order of markers not applying on top of a surface plot. It's the same as in @moble 's gist. |
The problem here is that the drawing order of the patches needs to change when the camera view changes so that the patches that are behind others wrt the line of sight get drawn first. The order of the patches is decided upon initializing the import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def get_camera_position(ax):
"""returns the camera position for 3D axes in cartesian coordinates"""
r = np.square(ax.xy_viewLim.max).sum()
theta, phi = np.radians((90 - ax.elev, ax.azim))
return np.array(sph2cart(r, theta, phi), ndmin=2).T
def sph2cart(r, theta, phi):
"""spherical to cartesian transformation."""
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
return x, y, z
def reorder_camera_distance():
"""
Sort the patches (via their offsets) by decreasing distance from camera position
so that the furthest gets drawn first.
"""
# camera position in xyz
camera = get_camera_position(ax)
# distance of patches from camera
d = np.square(np.subtract(camera, patches._offsets3d)).sum(0)
o = d.argsort()[::-1]
patches._offsets3d = tuple(np.array(patches._offsets3d)[:, o])
patches._facecolor3d = patches._facecolor3d[o]
patches._edgecolor3d = patches._edgecolor3d[o]
# todo: similar for linestyles, linewidths, etc....
def on_draw(event):
reorder_camera_distance()
# MWE
x = np.array([-1, 0, 1])
y = np.array([0, 0, 0])
z = y
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
patches = ax.scatter(x, y, z, s=2500, c=x)
# connect to draw event (re-orders patches each time the canvas draws)
fig.canvas.mpl_connect('draw_event', on_draw) Things should now be drawn in the correct order irrespective of the viewing angle. |
Might be addressed by #15594. |
Indeed, I wasn't aware of that PR since it wasn't linked here, but from what I can tell it should fix this issue. |
|
@ImportanceOfBeingErnest Yes, I personally find this section of code quite opaque, partly due to the choice of variable names, sparse comments and lack of docstrings... |
Should be fixed by #17378. |
I understand that 3d plotting is as yet a bit hacky, but from what little I understand of the code for
Path3DCollection
(or is itPatch3DCollection
as the docstring for scatter says?), it feels like this should be fixable (patches aren't intersecting each other here).I'm using Matplotlib v1.5.0. The zorder of markers plotted by 3d scatter goes wrong in certain views.
Here's a minimal example:
I've made the marker sizes really huge to illustrate the point clearly - in the initial view, the overlap is correct (in this example), but on rotation, it goes wrong:


It's not fully clear because of the depthshading, but red is on top of green, which is on top of blue.
It definitely looks like some zorder information is present - depthshade is using it correctly - but it's not being used to update the drawing order correctly.
This problem gets severely exacerbated as the number of scatter points increases. To demonstrate, execution of this gist produces the following ugly result:

The sphere ought to be mostly red (it's the top view of a spherical grid with color=z-coordinate), instead it's mostly blue because of poor ordering. Rotating it will make it seem like there are holes in the sphere at different places because of incorrect overlap in some strange patterns.
The text was updated successfully, but these errors were encountered: