Search code examples
pythonaxis-labels

Shift tick label in 3D plot in python


In my figure it is not immediately clear to which dash at the axis my values belong, it needs a closer look. I want it to be clear right away.

Now I have added in red to which they belong. When the values are shifted a little bit upwards I think it will be more clear.

This explains my problem

This is my code

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x =  np.arange(1950,2010,1)
y = np.arange(0, 50,1)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(y,x) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)

ax.plot_wireframe(X, Y, Z)

ax.view_init(25,-30)
plt.xlabel('Year')

plt.show()

Thanks


Solution

  • The correct solution would be to make use of the tick_params function on the Axes3D object as follows:

    fig = plt.figure()
    ax = Axes3D(fig)
    
    ax.minorticks_on()
    ax.tick_params(axis='both', width=10, labelsize=10, pad=0)
    
    x =  np.arange(1950,2010,1)
    y = np.arange(0, 50,1)
    X, Y = np.meshgrid(x, y)
    zs = np.array([fun(y,x) for x,y in zip(np.ravel(X), np.ravel(Y))])
    Z = zs.reshape(X.shape)
    
    ax.plot_wireframe(X, Y, Z)
    ax.view_init(25,-30)
    plt.xlabel('Year')
    
    plt.show()
    

    In particular, the tick_params function has a pad parameter to control the distance. Unfortunately, this does not seem to be fully implemented yet:

    Note

    While this function is currently implemented, the core part of the Axes3D object may ignore some of these settings. Future releases will fix this. Priority will be given to those who file bugs.