Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

label manipulation for 3d plot using matplotlib

I came up with the following code to produce a figure in python+matplotlib:

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,1, projection='3d')

ax.plot_surface(KX[kxl3d:kxr3d,kxl3d:kxr3d], KY[kxl3d:kxr3d,kxl3d:kxr3d],
                BLP[kxl3d:kxr3d,kxl3d:kxr3d], rstride=8, cstride=8, alpha=0.4)

for idx in range(3):
    ax.plot(kx[x_points]+momentum_spi[idx,0], ky[y_points]+momentum_spi[idx,1],
            energy_spi[idx], linestyle='none', marker='o', 
            markerfacecolor=color_spi[idx], markersize=5)

ax.set_xlim(kl3d, kr3d)
ax.set_ylim(kl3d, kr3d)

ax.set_xlabel(r'$k_x[\mu m^{-1}]$')
ax.set_ylabel(r'$k_y[\mu m^{-1}]$')
ax.set_zlabel(r'$\epsilon-\omega_X[\gamma_p]$')

The output is: 3d_rings

My question is, how can I

  1. move the z axis label and tick labels to the left hand side of the figure, so that they are not overwritten by the rings and
  2. increase the space between the x and y tick labels and the axes labels (notice they overlap a little, especially for the y axis).
like image 515
Andrei Berceanu Avatar asked Jan 11 '23 02:01

Andrei Berceanu


1 Answers

You can snap zaxis to the left with the code posted here:

tmp_planes = ax.zaxis._PLANES 
ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3], 
                     tmp_planes[0], tmp_planes[1], 
                     tmp_planes[4], tmp_planes[5])
view_1 = (25, -135)
view_2 = (25, -45)
init_view = view_2
ax.view_init(*init_view)

You can control the distance of label to the axis, by adding a new line, and setting linespacing:

ax.set_xlabel('\n' + 'xlabel', linespacing=4)

Here's a complete example:

#!/usr/bin/python3

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

import numpy as np

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

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

tmp_planes = ax.zaxis._PLANES 
ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3], 
                     tmp_planes[0], tmp_planes[1], 
                     tmp_planes[4], tmp_planes[5])
view_1 = (25, -135)
view_2 = (25, -45)
init_view = view_2
ax.view_init(*init_view)

ax.plot_surface(X, Y, Z)

ax.set_xlabel('\n' + 'xlabel', linespacing=4)
ax.set_ylabel('ylabel')

fig.tight_layout()

fig.savefig('test.png')

enter image description here

(Note though that the zx and zy grid goes on the wrong side of the box. I don't know how to fix that).

like image 149
Adobe Avatar answered Jan 22 '23 09:01

Adobe