Skip to content

scatter3D assigns wrong color to points for some plot orientations #18287

Closed
@MarkusSchwarz1980

Description

@MarkusSchwarz1980

Bug report

Bug summary

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.

import numpy as np
from matplotlib import pyplot as plt

import matplotlib
backend = matplotlib.get_backend()

# two view points to show the problem
view_points = [[15, -70],[15, -110]]

# create points along the x-axis
num_points, x_max = 10, 4
x_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])

for view_point in view_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-coordinate
    scatter = ax3d.scatter3D(*coords_3d, c=x_coord, depthshade=False)

    # draw same points using `plot` and offset by 1 in z-direction for clarity
    for coord in coords_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

Qt5Agg_-110

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

Qt5Agg_-70

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
  • Python version: 3.7.8
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions