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

Skip to content

FIX label vertical alignment can now be specified #8081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
NelleV committed Feb 15, 2017
commit e274ed9c5d13f13b44496b53f54dcca10f827091
4 changes: 4 additions & 0 deletions doc/users/dflt_style_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ values is a single line of python

mpl.style.use('classic')

Note that to revert the position of tick labels, you need in addition to set
the rcParams `ytick.alignment` to "center".

See :ref:`customizing-with-matplotlibrc-files` for details about how to
persistently and selectively revert many of these changes.


.. contents:: Table of Contents
:depth: 2
:local:
Expand Down
23 changes: 13 additions & 10 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,12 @@ def get_xaxis_text1_transform(self, pad_points):
place axis elements in different locations.

"""
labels_align = matplotlib.rcParams["xtick.alignment"]

return (self.get_xaxis_transform(which='tick1') +
mtransforms.ScaledTranslation(0, -1 * pad_points / 72.0,
self.figure.dpi_scale_trans),
"top", "center")
"top", labels_align)

def get_xaxis_text2_transform(self, pad_points):
"""
Expand All @@ -757,10 +759,11 @@ def get_xaxis_text2_transform(self, pad_points):
place axis elements in different locations.

"""
labels_align = matplotlib.rcParams["xtick.alignment"]
return (self.get_xaxis_transform(which='tick2') +
mtransforms.ScaledTranslation(0, pad_points / 72.0,
self.figure.dpi_scale_trans),
"bottom", "center")
"bottom", labels_align)

def get_yaxis_transform(self, which='grid'):
"""
Expand Down Expand Up @@ -808,10 +811,11 @@ def get_yaxis_text1_transform(self, pad_points):
place axis elements in different locations.

"""
labels_align = matplotlib.rcParams["ytick.alignment"]
return (self.get_yaxis_transform(which='tick1') +
mtransforms.ScaledTranslation(-1 * pad_points / 72.0, 0,
self.figure.dpi_scale_trans),
"center_baseline", "right")
labels_align, "right")

def get_yaxis_text2_transform(self, pad_points):
"""
Expand All @@ -834,10 +838,12 @@ def get_yaxis_text2_transform(self, pad_points):
place axis elements in different locations.

"""
labels_align = matplotlib.rcParams["ytick.alignment"]

return (self.get_yaxis_transform(which='tick2') +
mtransforms.ScaledTranslation(pad_points / 72.0, 0,
self.figure.dpi_scale_trans),
"center_baseline", "left")
labels_align, "left")

def _update_transScale(self):
self.transScale.set(
Expand Down Expand Up @@ -2560,13 +2566,10 @@ def ticklabel_format(self, **kwargs):
raise ValueError("scilimits must be a sequence of 2 integers")
if style[:3] == 'sci':
sb = True
elif style in ['plain', 'comma']:
elif style == 'plain':
sb = False
if style == 'plain':
cb = False
else:
cb = True
raise NotImplementedError("comma style remains to be added")
elif style == 'comma':
raise NotImplementedError("comma style remains to be added")
elif style == '':
sb = None
else:
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ def validate_font_properties(s):
'verbose',
['silent', 'helpful', 'debug', 'debug-annoying'])

_validate_alignment = ValidateInStrings(
'alignment',
['center', 'top', 'bottom', 'baseline',
'center_baseline'])

def validate_whiskers(s):
if s=='range':
return 'range'
Expand Down Expand Up @@ -1214,6 +1219,9 @@ def validate_animation_writer_path(p):
# fontsize of the ytick labels
'ytick.labelsize': ['medium', validate_fontsize],
'ytick.direction': ['out', six.text_type], # direction of yticks
'ytick.alignment': ["center_baseline", _validate_alignment],
'xtick.alignment': ["center", _validate_alignment],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in the xtick section above.



'grid.color': ['#b0b0b0', validate_color], # grid color
'grid.linestyle': ['-', six.text_type], # solid
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,15 @@ def test_bar_tick_label_multiple():
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
align='center')

@image_comparison(baseline_images=['bar_tick_label_multiple_old_alignment'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bar_tick_label_multiple_old_label_alignment (cf. the baseline image name)?

extensions=['png'])
def test_bar_tick_label_multiple_old_alignment():
# From 2516: plot bar with array of string labels for x axis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't really have anything to do with #2516.

matplotlib.rcParams["ytick.alignment"] = "center"
ax = plt.gca()
ax.bar([1, 2.5], [1, 2], width=[0.2, 0.5], tick_label=['a', 'b'],
align='center')


@image_comparison(baseline_images=['barh_tick_label'],
extensions=['png'])
Expand Down