Home > Software engineering >  How to remove offsets in 3D projection plot?
How to remove offsets in 3D projection plot?

Time:03-06

I realized a slight "misalignment" of a plot I'm making in 3D with matplotlib. Here is an MWE:

import numpy as np
from matplotlib import pyplot as plt
figure = plt.figure(figsize=(8,10.7))
ax = plt.gca(projection='3d')
ax.plot_surface(np.array([[0, 0], [30, 30]]),
                np.array([[10, 10], [10, 10]]),
                np.array([[10, 20], [10, 20]]), 
                rstride=1, cstride=1
)
ax.plot_surface(np.array([[0, 0], [30, 30]]),
                np.array([[20, 20], [20, 20]]),
                np.array([[10, 20], [10, 20]]), 
                rstride=1, cstride=1
)
plt.show()
plt.close()

Clearly, the bins are not correctly centered, as the surfaces seem to start at 10.5 and end at 20.5 instead of 10 and 20 sharply. How could one achieve the latter?

enter image description here

CodePudding user response:

This is caused by matplotlib's processing of 3D Axis coordinates. It deliberately shifts mins and maxs to create some artificial padding:

unpadded 3d plot

  • Related