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

Skip to content

Commit e274ed9

Browse files
committed
FIX tick label alignment can now be specified
This patch adds two new rcParams allowing to set label alignment. The sole reason for the existance of these new parameters is to allow user to reset the style to before 2.0 for testing purposes. More specifically, ytick horizontal alignement was changed in a non backward compatible way. xtick vertical alignement was added for API consistency. closes #7905
1 parent ac51372 commit e274ed9

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

doc/users/dflt_style_changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ values is a single line of python
1515
1616
mpl.style.use('classic')
1717
18+
Note that to revert the position of tick labels, you need in addition to set
19+
the rcParams `ytick.alignment` to "center".
20+
1821
See :ref:`customizing-with-matplotlibrc-files` for details about how to
1922
persistently and selectively revert many of these changes.
2023

24+
2125
.. contents:: Table of Contents
2226
:depth: 2
2327
:local:

lib/matplotlib/axes/_base.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,12 @@ def get_xaxis_text1_transform(self, pad_points):
731731
place axis elements in different locations.
732732
733733
"""
734+
labels_align = matplotlib.rcParams["xtick.alignment"]
735+
734736
return (self.get_xaxis_transform(which='tick1') +
735737
mtransforms.ScaledTranslation(0, -1 * pad_points / 72.0,
736738
self.figure.dpi_scale_trans),
737-
"top", "center")
739+
"top", labels_align)
738740

739741
def get_xaxis_text2_transform(self, pad_points):
740742
"""
@@ -757,10 +759,11 @@ def get_xaxis_text2_transform(self, pad_points):
757759
place axis elements in different locations.
758760
759761
"""
762+
labels_align = matplotlib.rcParams["xtick.alignment"]
760763
return (self.get_xaxis_transform(which='tick2') +
761764
mtransforms.ScaledTranslation(0, pad_points / 72.0,
762765
self.figure.dpi_scale_trans),
763-
"bottom", "center")
766+
"bottom", labels_align)
764767

765768
def get_yaxis_transform(self, which='grid'):
766769
"""
@@ -808,10 +811,11 @@ def get_yaxis_text1_transform(self, pad_points):
808811
place axis elements in different locations.
809812
810813
"""
814+
labels_align = matplotlib.rcParams["ytick.alignment"]
811815
return (self.get_yaxis_transform(which='tick1') +
812816
mtransforms.ScaledTranslation(-1 * pad_points / 72.0, 0,
813817
self.figure.dpi_scale_trans),
814-
"center_baseline", "right")
818+
labels_align, "right")
815819

816820
def get_yaxis_text2_transform(self, pad_points):
817821
"""
@@ -834,10 +838,12 @@ def get_yaxis_text2_transform(self, pad_points):
834838
place axis elements in different locations.
835839
836840
"""
841+
labels_align = matplotlib.rcParams["ytick.alignment"]
842+
837843
return (self.get_yaxis_transform(which='tick2') +
838844
mtransforms.ScaledTranslation(pad_points / 72.0, 0,
839845
self.figure.dpi_scale_trans),
840-
"center_baseline", "left")
846+
labels_align, "left")
841847

842848
def _update_transScale(self):
843849
self.transScale.set(
@@ -2560,13 +2566,10 @@ def ticklabel_format(self, **kwargs):
25602566
raise ValueError("scilimits must be a sequence of 2 integers")
25612567
if style[:3] == 'sci':
25622568
sb = True
2563-
elif style in ['plain', 'comma']:
2569+
elif style == 'plain':
25642570
sb = False
2565-
if style == 'plain':
2566-
cb = False
2567-
else:
2568-
cb = True
2569-
raise NotImplementedError("comma style remains to be added")
2571+
elif style == 'comma':
2572+
raise NotImplementedError("comma style remains to be added")
25702573
elif style == '':
25712574
sb = None
25722575
else:

lib/matplotlib/rcsetup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ def validate_font_properties(s):
465465
'verbose',
466466
['silent', 'helpful', 'debug', 'debug-annoying'])
467467

468+
_validate_alignment = ValidateInStrings(
469+
'alignment',
470+
['center', 'top', 'bottom', 'baseline',
471+
'center_baseline'])
472+
468473
def validate_whiskers(s):
469474
if s=='range':
470475
return 'range'
@@ -1214,6 +1219,9 @@ def validate_animation_writer_path(p):
12141219
# fontsize of the ytick labels
12151220
'ytick.labelsize': ['medium', validate_fontsize],
12161221
'ytick.direction': ['out', six.text_type], # direction of yticks
1222+
'ytick.alignment': ["center_baseline", _validate_alignment],
1223+
'xtick.alignment': ["center", _validate_alignment],
1224+
12171225

12181226
'grid.color': ['#b0b0b0', validate_color], # grid color
12191227
'grid.linestyle': ['-', six.text_type], # solid
7.89 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,15 @@ def test_bar_tick_label_multiple():
11431143
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
11441144
align='center')
11451145

1146+
@image_comparison(baseline_images=['bar_tick_label_multiple_old_alignment'],
1147+
extensions=['png'])
1148+
def test_bar_tick_label_multiple_old_alignment():
1149+
# From 2516: plot bar with array of string labels for x axis
1150+
matplotlib.rcParams["ytick.alignment"] = "center"
1151+
ax = plt.gca()
1152+
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
1153+
align='center')
1154+
11461155

11471156
@image_comparison(baseline_images=['barh_tick_label'],
11481157
extensions=['png'])

0 commit comments

Comments
 (0)