Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tick label positions for matplotlib 3D plot

I am trying to work out how to set/correct the position of tick labels for a 3D matplotlib plot. Tick labels do not align with the ticks. The issue seems to be especially prominent when many tick labels are required.

I have modified an example (http://matplotlib.org/examples/mplot3d/polys3d_demo.html) from the matplotlib documentation to illustrate my question.

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')

cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)

xs = np.arange(0, 10, 0.4)
verts = []
zs = np.arange(50)
for z in zs:
    ys = np.ones(len(xs))*z
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts,facecolor='c')
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, len(zs))
ax.set_yticks(np.arange(len(zs)))
labels = {}
for l_c in zs:
    labels[l_c] = 'This Looks Bad'
ax.set_yticklabels(labels,rotation=-15)
ax.set_zlabel('Z')
ax.set_zlim3d(0, ys.max())

plt.show()

enter image description here

So the question is: how can I get the tick labels to align with the tick positions?

like image 586
random.me Avatar asked Apr 17 '13 13:04

random.me


People also ask

How do I change the position of a label in Matplotlib?

With matplotlib version 3.3. 0, the matplotlib functions set_xlabel and set_ylabel have a new parameter “loc” that can help adjust the positions of axis labels. For the x-axis label, it supports the values 'left', 'center', or 'right' to place the label towards left/center/right.

How do you add labels in 3D plots?

Use the Layer Contents dialog or Plot Setup dialog to add the point as 3D Scatter to the surface plot. Open the Plot Details dialog and go to the Label tab of the 3D scatter plot, check Enable to activate the data labels and do further customization.

How do I center a tick label in Matplotlib?

Set the locator of the major/minor ticker using set_major_locator() and set_minor_locator() methods. Set the locator of the major/minor formatter using set_major_locator() and set_minor_formatter() methods. Now, place the ticklabel at the center. To display the figure, use show() method.


2 Answers

By using these alignments, I get much better placements:

ax.set_yticklabels(labels,rotation=-15,
                   verticalalignment='baseline',
                   horizontalalignment='left')

I've modified the example with less tick markers so you can see the placement:

enter image description here

like image 125
Hooked Avatar answered Oct 05 '22 20:10

Hooked


You can also set the pad argument as negative in the tick_params options for each axis. Like this:

    ax.tick_params(axis='x', which='major', pad=-3)

This might help to adjust the distance between tick labels and axis.

like image 23
MHDF Avatar answered Oct 05 '22 19:10

MHDF