Search code examples
pythonmatplotlibtkintermatplotlib-3d

matplotlib 3D scatter points not placed correctly


I'm trying to do some simple 3D plots but the scatter points appear not to align properly with the axes. This graph should have points aligned with x=1, y=1, z=1. This issue also occurs using the tkinter backend.

MATPLOTLIB 3D scatter

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.array([-1, -0.5, 0, 0.5, 1]) 
y = np.array([-1, -0.5, 0, 0.5, 1]) 
z = np.array([-1, -0.5, 0, 0.5, 1]) 

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

xyz = np.meshgrid(x,y,z)
ax.scatter(xyz[0], xyz[1], xyz[2], marker='.')

plt.show()

The following links seem loosely related, but I'm unable to put together a concrete solution.

https://github.com/matplotlib/matplotlib/issues/11836

https://github.com/matplotlib/matplotlib/issues/1077/

Python 3.11.1, Matplotlib 3.7.1, Windows 10


Solution

  • As pointed by @jared in comment, points are aligned as expected. You can see it by rotating the graph. Here is another view, using a copy paste of the code you provided, after rotating the figure to face one of the axis: rotated graph .