From ee8d3e77b361ba686617126193d4ffbb89ebd61f Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Wed, 6 Mar 2019 22:57:44 +0100 Subject: [PATCH 1/2] Update centered ticklabels example --- .../ticks_and_spines/centered_ticklabels.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/ticks_and_spines/centered_ticklabels.py b/examples/ticks_and_spines/centered_ticklabels.py index 308ac3bb1121..404c9b2addad 100644 --- a/examples/ticks_and_spines/centered_ticklabels.py +++ b/examples/ticks_and_spines/centered_ticklabels.py @@ -1,19 +1,20 @@ """ -=================== -Centered Ticklabels -=================== +============================== +Centering labels between ticks +============================== -sometimes it is nice to have ticklabels centered. Matplotlib currently -associates a label with a tick, and the label can be aligned -'center', 'left', or 'right' using the horizontal alignment property:: +Ticklabels are aligned relative to their associated tick. The alignment +'center', 'left', or 'right' can be controlled using the horizontal alignment +property:: for label in ax.xaxis.get_xticklabels(): label.set_horizontalalignment('right') -but this doesn't help center the label between ticks. One solution -is to "fake it". Use the minor ticks to place a tick centered -between the major ticks. Here is an example that labels the months, -centered between the ticks +However there is no direct way to center the labels between ticks. To fake +this behavior, one can place a label on the minor ticks in between the major +ticks, and hide the major tick labels and minor ticks. + +Here is an example that labels the months, centered between the ticks. """ import numpy as np @@ -34,7 +35,7 @@ ax.plot(date, r.adj_close) ax.xaxis.set_major_locator(dates.MonthLocator()) -ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15)) +ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) From 4dd06fb8b70c94a548beef13567c930ba5922e20 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 7 Mar 2019 02:19:32 +0100 Subject: [PATCH 2/2] Update examples/ticks_and_spines/centered_ticklabels.py Co-Authored-By: timhoffm <2836374+timhoffm@users.noreply.github.com> --- examples/ticks_and_spines/centered_ticklabels.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ticks_and_spines/centered_ticklabels.py b/examples/ticks_and_spines/centered_ticklabels.py index 404c9b2addad..d6d80d916e5a 100644 --- a/examples/ticks_and_spines/centered_ticklabels.py +++ b/examples/ticks_and_spines/centered_ticklabels.py @@ -35,6 +35,7 @@ ax.plot(date, r.adj_close) ax.xaxis.set_major_locator(dates.MonthLocator()) +# 16 is a slight approximation since months differ in number of days. ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16)) ax.xaxis.set_major_formatter(ticker.NullFormatter())