5

I am using mplot3d from the mpl_toolkits library. When displaying the 3D surface on the figure I'm realized the axis were not positioned as I wished they would.

Let me show, I have added to the following screenshot the position of each axis:

enter image description here

Is there a way to change the position of the axes in order to get this result:

enter image description here

Here's the working code:

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

ax = Axes3D(plt.figure())
def f(x,y) :
    return -x**2 - y**2

X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)

# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])

ax.set_yticklabels([-1,1],rotation=-15, va='center', ha='right')

plt.show()

I have tried using xaxis.set_ticks_position('left') statement, but it doesn't work.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Ivan
  • 34,531
  • 8
  • 55
  • 100

1 Answers1

3

No documented methods, but with some hacking ideas from https://stackoverflow.com/a/15048653/1149007 you can.

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

fig = plt.figure()
ax = ax = fig.add_subplot(111, projection='3d')
ax.view_init(30, 30)


def f(x,y) :
    return -x**2 - y**2

X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)

# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])

ax.xaxis._axinfo['juggled'] = (0,0,0)
ax.yaxis._axinfo['juggled'] = (1,1,1)
ax.zaxis._axinfo['juggled'] = (2,2,2)

plt.show()

enter image description here

I can no idea of the meaning of the third number in triples. If set zeros nothing changes in the figure. So should look in the code for further tuning.

You can also look at related question Changing position of vertical (z) axis of 3D plot (Matplotlib)? with low level hacking of _PLANES property.

sherdim
  • 1,159
  • 8
  • 19