You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
scatter3D assigns wrong color to points for some plot orientations #18287
When colors are based on coordinates in scatter3D, the color changes when the orientation of the plot is changed.
Code for reproduction
This codes plots 10 points along the x-axis from 0 to x_max, using scatter3D with their color depending on their x-position (dark purple for 0, yellow for x_max). The first view_point gives the expected result, whereas the second does not. For comparison, the same data plotted with plot (and offset by 1 in the z-direction for clarity) always give the expected result.
importnumpyasnpfrommatplotlibimportpyplotaspltimportmatplotlibbackend=matplotlib.get_backend()
# two view points to show the problemview_points= [[15, -70],[15, -110]]
# create points along the x-axisnum_points, x_max=10, 4x_coord=np.linspace(0, x_max, num_points)
y_coord=np.zeros_like(x_coord)
z_coord=np.zeros_like(x_coord)
coords_3d=np.array([x_coord, y_coord, z_coord])
forview_pointinview_points:
fig=plt.figure(f'view point {view_point}', clear=True)
ax3d=fig.gca(projection='3d')
ax3d.set_title(f'backend {backend}, view point {view_point}')
ax3d.set_xlabel('x')
ax3d.set_ylabel('y')
ax3d.set_zlabel('z')
# color points according to x-coordinatescatter=ax3d.scatter3D(*coords_3d, c=x_coord, depthshade=False)
# draw same points using `plot` and offset by 1 in z-direction for clarityforcoordincoords_3d.T:
ax3d.plot(*coord+ [0,0,1], 'x',
color=scatter.cmap(scatter.norm(coord[0])) )
fig.colorbar(scatter, label='x-coordinate')
ax3d.view_init(*view_point)
ax3d.legend(ax3d.get_children()[:2], ['scatter','plot'])
fig.tight_layout()
plt.show()
Actual outcome
The view_point=[15,-110] assigns the color in the wrong (reversed) order: the point at (0,0,0) has the bright color corresponding to an x-coordinate of x_max, which is clearly wrong. This occurs for azimuth angles between [-180,-90] and [90, 180] degrees.
Expected outcome
The view_point=[15,-70] produces the expected outcome, i.e. dark colors for points close to the origin at (0,0,0) and bright colors for points farther away. This should be independent on the orientation of the plot.
Matplotlib version
Operating system: maxOS 10.15.6 (19G2021)
Matplotlib version: 3.3.1
Matplotlib backend (print(matplotlib.get_backend())): tested with Qt5Agg and MacOSX
It is indeed caused by the merge commit from #17378, though the fix in #18135 was to start sorting sizes, while facecolors and edgecolors are already sorted the same way.
Activity
timhoffm commentedon Aug 18, 2020
Maybe introduced via #17378? This sounds similar to #18135.
QuLogic commentedon Aug 19, 2020
It is indeed caused by the merge commit from #17378, though the fix in #18135 was to start sorting sizes, while facecolors and edgecolors are already sorted the same way.
QuLogic commentedon Aug 19, 2020
Oops, sorry, I was mislead by
view_initnot setting the Axes stale correctly. That is not the commit that introduces the issue. It is actually #18156.Correctly re-order scatter3d face/edgecolors.