I'm attempting to plot 2D data onto a 3D axis.
I have the 3D shape working using ax.plot_surface
but I'm unable to get the 2D data to sit flush against the axis walls using ax.plot
.
Here is a stripped down example code showing the issue I'm having with the 2D data:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate Example Data
x = [0.04,0,-0.04]
y = [0.04,0,-0.04]
z = [0.04,0,-0.04]
# Start plotting environment
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot 3 lines positioned against the axes "walls"
ax.plot(x,y,-0.08,zdir='z',c='r')
ax.plot(x,z, 0.08,zdir='y',c='g')
ax.plot(y,z,-0.08,zdir='x',c='b')
# Label each axis
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# Set each axis limits
ax.set_xlim([-0.08,0.08])
ax.set_ylim([-0.08,0.08])
ax.set_zlim([-0.08,0.08])
# Equally stretch all axes
ax.set_aspect("equal")
# Set plot size for saving to disk
plt.gcf().set_size_inches(11.7,8.3)
# Save figure in .eps and .png format
plt.savefig('test.eps', format='eps')
plt.savefig('test.png', format='png', dpi=300)
# Display figure
plt.show()
This gives the following result from which you might be able to see that the ends of the data lines don't sit on the axis lines (ie. don't line up with 0.04 and -0.04):
By interactively exploring the plot I determined that changing the 0.08's in the ax.plot
calls, to a magnitude of 0.083 (while keeping the relevant signs) allows the plots to fit much flatter to the wall.
My interpretation of this is that the plot isn't enforcing my axis limits which appears evident on the plot by looking at the spacings where the axes meet, but using ax.get_xlim()
etc, shows the values that I set so I'm missing something.
Any ideas on how I can get these plots to sit flush against the walls?
Many Thanks,
Tim
Edit:
I've also attempted to set the axis limits using
ax.set_xlim3d(-0.08,0.08)
ax.set_ylim3d(-0.08,0.08)
ax.set_zlim3d(-0.08,0.08)
and
ax.set_xlim3d([-0.08,0.08])
ax.set_ylim3d([-0.08,0.08])
ax.set_zlim3d([-0.08,0.08])
without any luck.
I'm definitely leaning towards attributing this to a padding issue where the axes meet but I'm unable to find any documentation regarding this. ie. I'm setting the plot position to -0.08 and the axis limit to -0.08 but the plot is adding a little padding at the end to make the limits somewhere between -0.082 and -0.083.
I either want to remove the padding or get the padding value so that I can input it into ax.plot
command.
Edit2:
Somebody else who has encountered this issue but hasn't had the issue resolved Changing the position of the grid walls in an mplot3d figure