2

How can I place the axis of a 3D plot inside the graph itself instead of on the graph edges?

I need this:

drawing_with_axes_inside


Instead of my current axis which are by default on the edges of the plot box:

current_graph_with_axes_on_edges

Code:

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


fig = plt.figure()
ax = fig.add_subplot(2, 1, 1, projection='3d')
x = np.linspace(-1, 1, 300)


# equivalent to f=R(x)*e^ix
f = np.e**(15*1j*x - 18 * x**4)
real_f = np.real(f)
im_f = np.imag(f)
f_conj = np.conjugate(f)
im_f_conj = np.imag(f_conj)

# 1st plot
ax.scatter(x, real_f, im_f, label='Ψ ', color='b')
plt.title("The complex conjugate of Ψ\nis its mirror image.")

ax.legend()
ax.set_xlabel("x")
ax.set_ylabel("Re")
ax.set_zlabel("Im")
# ax.set_axis_off() removes the box as well
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

# 2nd plot
ax = fig.add_subplot(2, 1, 2, projection='3d')
ax.scatter(x, real_f, im_f_conj, label='Ψ *', color='r')

ax.legend()
ax.set_xlabel("x")
ax.set_ylabel("Re")
ax.set_zlabel("Im")
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

plt.show()
user
  • 5,370
  • 8
  • 47
  • 75
  • 1
    Probably related: [this](https://stackoverflow.com/q/48442713/2454357) and [this](https://stackoverflow.com/q/51018584/2454357) – Thomas Kühn Feb 18 '19 at 11:41

1 Answers1

3

As it currently stands there is no supported implementation for 3D plots to do this (canonical methods and issues were pointed out by @ Thomas Kühn. A naive approach and uggly approach would be to just plot lines as axes, however this will have no ticks on them.

Edit: one could add text for axes limits

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


fig = plt.figure(figsize = (20,20))
ax = fig.add_subplot(2, 1, 1, projection='3d')
x = np.linspace(-1, 1, 300)


# equivalent to f=R(x)*e^ix
f = np.e**(15*1j*x - 18 * x**4)
real_f = np.real(f)
im_f = np.imag(f)

# 1st plot
ax.scatter(x, real_f, im_f, label='Ψ ', color='b')
plt.title("The complex conjugate of Ψ\nis its mirror image.")

ax.legend()
ax.set_xlabel("x")
ax.set_ylabel("Re")
ax.set_zlabel("Im")
# ax.set_axis_off() removes the box as well
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])


ax.plot([x.min(), x.max()], [0,0], [0,0], color = 'black')
ax.plot([0, 0], [real_f.min(), real_f.max()], [0, 0], color = 'black')
ax.plot([0, 0], [0,0], [im_f.min(), im_f.max()], color = 'black')

ax.text(0,0,0, '0')
ax.text(x.max(),0,0, x.max())
ax.text(x.min(), 0, 0, x.min())

ax.text(0, real_f.max(), 0, real_f.max().round(2))
ax.text(0, real_f.min(), 0, real_f.min().round(2))

ax.text(0, 0, im_f.max(), imf_f.max().round(2))
ax.text(0, 0, imf_min(), im_f.min().round(2))
ax.axis('off')
plt.show()

enter image description here

cvanelteren
  • 1,633
  • 9
  • 16