Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36b0f23

Browse files
committed
Merge pull request matplotlib#3787 from jbbrokaw/axes3d-label-padding
Refactors axis3d.py to address issue matplotlib#3610
2 parents f4f872b + c22e057 commit 36b0f23

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

doc/users/whats_new/axis3d.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fixed labelpad in Axis3D
2+
```````````````````````````````````
3+
4+
Axis3D now looks at xaxis.labelpad (from rcParams or set by
5+
set_xlabel('X LABEL', labelpad=30) or ax.zaxis.labelpad = 20)
6+
to determine the position of axis labels in 3D plots.

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,11 +1183,7 @@ def set_zlabel(self, zlabel, fontdict=None, labelpad=None, **kwargs):
11831183
'''
11841184
Set zlabel. See doc for :meth:`set_ylabel` for description.
11851185
1186-
.. note::
1187-
Currently, *labelpad* does not have an effect on the labels.
11881186
'''
1189-
# FIXME: With a rework of axis3d.py, the labelpad should work again
1190-
# At that point, remove the above message in the docs.
11911187
if labelpad is not None : self.zaxis.labelpad = labelpad
11921188
return self.zaxis.set_label_text(zlabel, fontdict, **kwargs)
11931189

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,10 @@ def __init__(self, adir, v_intervalx, d_intervalx, axes, *args, **kwargs):
8080
# This is a temporary member variable.
8181
# Do not depend on this existing in future releases!
8282
self._axinfo = self._AXINFO[adir].copy()
83-
self._axinfo.update({'label' : {'space_factor': 1.6,
84-
'va': 'center',
83+
self._axinfo.update({'label' : {'va': 'center',
8584
'ha': 'center'},
8685
'tick' : {'inward_factor': 0.2,
8786
'outward_factor': 0.1},
88-
'ticklabel': {'space_factor': 0.7},
8987
'axisline': {'linewidth': 0.75,
9088
'color': (0, 0, 0, 1)},
9189
'grid' : {'color': (0.9, 0.9, 0.9, 1),
@@ -265,7 +263,14 @@ def draw(self, renderer):
265263

266264
lxyz = 0.5*(edgep1 + edgep2)
267265

268-
labeldeltas = info['label']['space_factor'] * deltas
266+
# A rough estimate; points are ambiguous since 3D plots rotate
267+
ax_scale = self.axes.bbox.size / self.figure.bbox.size
268+
ax_inches = np.multiply(ax_scale, self.figure.get_size_inches())
269+
ax_points_estimate = sum(72. * ax_inches)
270+
deltas_per_point = 48. / ax_points_estimate
271+
default_offset = 21.
272+
labeldeltas = (self.labelpad + default_offset) * deltas_per_point\
273+
* deltas
269274
axmask = [True, True, True]
270275
axmask[index] = False
271276
lxyz = move_from_center(lxyz, centers, labeldeltas, axmask)
@@ -394,8 +399,10 @@ def draw(self, renderer):
394399
renderer.M)
395400

396401
# Get position of label
397-
labeldeltas = [info['ticklabel']['space_factor'] * x for
398-
x in deltas]
402+
default_offset = 8. # A rough estimate
403+
labeldeltas = (tick.get_pad() + default_offset) * deltas_per_point\
404+
* deltas
405+
399406
axmask = [True, True, True]
400407
axmask[index] = False
401408
pos[tickdir] = edgep1[tickdir]

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,30 @@ def test_quiver3d_pivot_tail():
233233

234234
ax.quiver(x, y, z, u, v, w, length=0.1, pivot='tail')
235235

236+
237+
@image_comparison(baseline_images=['axes3d_labelpad'], extensions=['png'])
238+
def test_axes3d_labelpad():
239+
from nose.tools import assert_equal
240+
from matplotlib import rcParams
241+
242+
fig = plt.figure()
243+
ax = Axes3D(fig)
244+
# labelpad respects rcParams
245+
assert_equal(ax.xaxis.labelpad, rcParams['axes.labelpad'])
246+
# labelpad can be set in set_label
247+
ax.set_xlabel('X LABEL', labelpad=10)
248+
assert_equal(ax.xaxis.labelpad, 10)
249+
ax.set_ylabel('Y LABEL')
250+
ax.set_zlabel('Z LABEL')
251+
# or manually
252+
ax.yaxis.labelpad = 20
253+
ax.zaxis.labelpad = -40
254+
255+
# Tick labels also respect tick.pad (also from rcParams)
256+
for i, tick in enumerate(ax.yaxis.get_major_ticks()):
257+
tick.set_pad(tick.get_pad() - i * 5)
258+
259+
236260
if __name__ == '__main__':
237261
import nose
238262
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)