3

I'm trying to make a 3d plot in python with axis in the middle like so:

enter image description here

I've tried with matplotlib and plotly without success. Just to specify my problem, i don't want a plot like the following image, where you can see the axis are outside the data:

enter image description here

5
  • Is this what you're looking for.? stackoverflow.com/questions/48442713/… Commented Jun 25, 2018 at 8:57
  • @user3747724 Be specific, plotly or matplotlib?? Commented Jun 26, 2018 at 3:43
  • Thanks guys! @AshSharma not quite - the axis has to be in origo as i tried to show with the image. Maybe this is an better image: intmath.com/blog/wp-content/images/2014/11/… Commented Jun 27, 2018 at 5:31
  • @NarenMurali it doesn't matter, just the one where it is possible to do this :D Commented Jun 27, 2018 at 5:31
  • @user3747724 the answer in that link says it is not possible to move the axes within matplotlib for 3D plots. Although it is quite possible to do what you're looking for in 2D plots. Commented Jun 27, 2018 at 8:17

1 Answer 1

3

I could not find a way to do this either, so this is my work around:

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

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_aspect('equal') 

# Draw centered axes
val = [1,0,0]
labels = ['x', 'y', 'z']
colors = ['r', 'g', 'b']
for v in range(3):
    x = [val[v-0], -val[v-0]]
    y = [val[v-1], -val[v-1]]
    z = [val[v-2], -val[v-2]]
    ax.plot(x,y,z,'k-', linewidth=1)
    ax.text(val[v-0], val[v-1], val[v-2], labels[v], color=colors[v], fontsize=20)


# Hide everything else
# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
# make the panes transparent
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# Hide box axes
ax._axis3don = False

# Expand to remove white space
ax.set_xlim(np.array([-1,1])*.57)
ax.set_ylim(np.array([-1,1])*.57)
ax.set_zlim(np.array([-1,1])*.57)

plt.tight_layout()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.