Skip to content

Commit 6f2ddf3

Browse files
committed
Update example to use tick_params where possible
1 parent 0a4866f commit 6f2ddf3

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

examples/ticks/centered_ticklabels.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'center', 'left', or 'right' can be controlled using the horizontal alignment
88
property::
99
10-
for label in ax.xaxis.get_xticklabels():
10+
for label in ax.get_xticklabels():
1111
label.set_horizontalalignment('right')
1212
1313
However there is no direct way to center the labels between ticks. To fake
@@ -23,7 +23,7 @@
2323
import matplotlib.ticker as ticker
2424
import matplotlib.pyplot as plt
2525

26-
# load some financial data; Google's stock price
26+
# Load some financial data; Google's stock price
2727
r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
2828
.view(np.recarray))
2929
r = r[-250:] # get the last 250 days
@@ -38,11 +38,12 @@
3838
ax.xaxis.set_major_formatter(ticker.NullFormatter())
3939
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
4040

41-
for tick in ax.xaxis.get_minor_ticks():
42-
tick.tick1line.set_markersize(0)
43-
tick.tick2line.set_markersize(0)
44-
tick.label1.set_horizontalalignment('center')
41+
# Remove the tick lines
42+
ax.tick_params(axis='x', which='minor', tick1On=False, tick2On=False)
4543

44+
# Align the minor tick label
45+
for label in ax.get_xticklabels(minor=True):
46+
label.set_horizontalalignment('center')
4647
imid = len(r) // 2
4748
ax.set_xlabel(str(r.date[imid].item().year))
48-
plt.show()
49+
fig.show()

0 commit comments

Comments
 (0)