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

Skip to content

Commit 2c872e3

Browse files
authored
Merge pull request #8678 from QuLogic/tick_params
ENH/FIX: Use Axes.tick_params/Axis.set_tick_params more
2 parents 1571ebb + c6bbfe9 commit 2c872e3

22 files changed

+80
-92
lines changed

doc/faq/howto_faq.rst

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,34 @@ over so that the tick labels fit in the figure:
241241

242242
.. _howto-ticks:
243243

244-
Configure the tick linewidths
245-
-----------------------------
244+
Configure the tick widths
245+
-------------------------
246246

247-
In Matplotlib, the ticks are *markers*. All
248-
:class:`~matplotlib.lines.Line2D` objects support a line (solid,
249-
dashed, etc) and a marker (circle, square, tick). The tick linewidth
250-
is controlled by the "markeredgewidth" property::
247+
Wherever possible, it is recommended to use the :meth:`~Axes.tick_params` or
248+
:meth:`~Axis.set_tick_params` methods to modify tick properties::
251249

252250
import matplotlib.pyplot as plt
253-
fig = plt.figure()
254-
ax = fig.add_subplot(111)
251+
252+
fig, ax = plt.subplots()
253+
ax.plot(range(10))
254+
255+
ax.tick_params(width=10)
256+
257+
plt.show()
258+
259+
For more control of tick properties that are not provided by the above methods,
260+
it is important to know that in Matplotlib, the ticks are *markers*. All
261+
:class:`~matplotlib.lines.Line2D` objects support a line (solid, dashed, etc)
262+
and a marker (circle, square, tick). The tick width is controlled by the
263+
``"markeredgewidth"`` property, so the above effect can also be achieved by::
264+
265+
import matplotlib.pyplot as plt
266+
267+
fig, ax = plt.subplots()
255268
ax.plot(range(10))
256269

257270
for line in ax.get_xticklines() + ax.get_yticklines():
258-
line.set_markersize(10)
271+
line.set_markeredgewidth(10)
259272

260273
plt.show()
261274

examples/api/logos2.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ def add_polar_bar():
7171
bar.set_facecolor(cm.jet(r/10.))
7272
bar.set_alpha(0.6)
7373

74-
for label in ax.get_xticklabels() + ax.get_yticklabels():
75-
label.set_visible(False)
76-
77-
for line in ax.get_ygridlines() + ax.get_xgridlines():
78-
line.set_lw(0.8)
79-
line.set_alpha(0.9)
80-
line.set_ls('-')
81-
line.set_color('0.5')
74+
ax.tick_params(labelbottom=False, labeltop=False,
75+
labelleft=False, labelright=False)
76+
77+
ax.grid(lw=0.8, alpha=0.9, ls='-', color='0.5')
8278

8379
ax.set_yticks(np.arange(1, 9, 2))
8480
ax.set_rmax(9)

examples/axes_grid1/demo_axes_divider.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ def demo_locatable_axes_easy(ax):
7777

7878
plt.colorbar(im, cax=ax_cb)
7979
ax_cb.yaxis.tick_right()
80-
for tl in ax_cb.get_yticklabels():
81-
tl.set_visible(False)
82-
ax_cb.yaxis.tick_right()
80+
ax_cb.yaxis.set_tick_params(labelright=False)
8381

8482

8583
def demo_images_side_by_side(ax):
@@ -94,8 +92,7 @@ def demo_images_side_by_side(ax):
9492

9593
ax.imshow(Z, extent=extent, interpolation="nearest")
9694
ax2.imshow(Z, extent=extent, interpolation="nearest")
97-
for tl in ax2.get_yticklabels():
98-
tl.set_visible(False)
95+
ax2.yaxis.set_tick_params(labelleft=False)
9996

10097

10198
def demo():

examples/axes_grid1/scatter_hist.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
3232

3333
# make some labels invisible
34-
plt.setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(),
35-
visible=False)
34+
axHistx.xaxis.set_tick_params(labelbottom=False)
35+
axHisty.yaxis.set_tick_params(labelleft=False)
3636

3737
# now determine nice limits by hand:
3838
binwidth = 0.25
@@ -47,14 +47,8 @@
4747
# thus there is no need to manually adjust the xlim and ylim of these
4848
# axis.
4949

50-
#axHistx.axis["bottom"].major_ticklabels.set_visible(False)
51-
for tl in axHistx.get_xticklabels():
52-
tl.set_visible(False)
5350
axHistx.set_yticks([0, 50, 100])
5451

55-
#axHisty.axis["left"].major_ticklabels.set_visible(False)
56-
for tl in axHisty.get_yticklabels():
57-
tl.set_visible(False)
5852
axHisty.set_xticks([0, 50, 100])
5953

6054
plt.draw()

examples/axes_grid1/simple_axes_divider2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
ax[3].set_axes_locator(divider.new_locator(nx=2, nx1=4, ny=0))
2929

3030
for ax1 in ax:
31-
plt.setp(ax1.get_xticklabels()+ax1.get_yticklabels(),
32-
visible=False)
31+
ax1.tick_params(labelbottom=False, labelleft=False)
3332

3433
plt.show()

examples/axes_grid1/simple_axes_divider3.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
divider.set_aspect(1.)
3838

3939
for ax1 in ax:
40-
plt.setp(ax1.get_xticklabels()+ax1.get_yticklabels(),
41-
visible=False)
40+
ax1.tick_params(labelbottom=False, labelleft=False)
4241

4342
plt.show()

examples/misc/patheffect_demo.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
PathEffects.Stroke(linewidth=5, foreground="w"),
2323
PathEffects.Normal()])
2424

25-
ax1.grid(True, linestyle="-")
26-
2725
pe = [PathEffects.withStroke(linewidth=3,
2826
foreground="w")]
29-
for l in ax1.get_xgridlines() + ax1.get_ygridlines():
30-
l.set_path_effects(pe)
27+
ax1.grid(True, linestyle="-", path_effects=pe)
3128

3229
ax2 = plt.subplot(132)
3330
arr = np.arange(25).reshape((5, 5))

examples/misc/pythonic_matplotlib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
ax1.set_ylabel('1 Hz')
7171
ax1.set_title('A sine wave or two')
7272

73-
for label in ax1.get_xticklabels():
74-
label.set_color('r')
73+
ax1.xaxis.set_tick_params(labelcolor='r')
7574

7675

7776
ax2 = fig.add_subplot(212)

examples/subplots_axes_and_figures/axes_props.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,11 @@
1111

1212
t = np.arange(0.0, 2.0, 0.01)
1313
s = np.sin(2 * np.pi * t)
14+
1415
fig, ax = plt.subplots()
1516
ax.plot(t, s)
16-
ax.grid(True)
17-
18-
ticklines = ax.get_xticklines() + ax.get_yticklines()
19-
gridlines = ax.get_xgridlines() + ax.get_ygridlines()
20-
ticklabels = ax.get_xticklabels() + ax.get_yticklabels()
21-
22-
for line in ticklines:
23-
line.set_linewidth(3)
24-
25-
for line in gridlines:
26-
line.set_linestyle('-.')
2717

28-
for label in ticklabels:
29-
label.set_color('r')
30-
label.set_fontsize('medium')
18+
ax.grid(True, linestyle='-.')
19+
ax.tick_params(labelcolor='r', labelsize='medium', width=3)
3120

3221
plt.show()

examples/ticks_and_spines/centered_ticklabels.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
associates a label with a tick, and the label can be aligned
88
'center', 'left', or 'right' using the horizontal alignment property::
99
10-
11-
for label in ax.xaxis.get_xticklabels():
12-
label.set_horizontalalignment('right')
10+
ax.xaxis.set_tick_params(horizontalalignment='right')
1311
1412
but this doesn't help center the label between ticks. One solution
1513
is to "fake it". Use the minor ticks to place a tick centered

examples/ticks_and_spines/date_demo_rrule.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
plt.plot_date(dates, s)
3535
ax.xaxis.set_major_locator(loc)
3636
ax.xaxis.set_major_formatter(formatter)
37-
labels = ax.get_xticklabels()
38-
plt.setp(labels, rotation=30, fontsize=10)
37+
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
3938

4039
plt.show()

examples/userdemo/demo_gridspec01.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
def make_ticklabels_invisible(fig):
1111
for i, ax in enumerate(fig.axes):
1212
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
13-
for tl in ax.get_xticklabels() + ax.get_yticklabels():
14-
tl.set_visible(False)
13+
ax.tick_params(labelbottom=False, labelleft=False)
1514

1615

1716
fig = plt.figure(0)

examples/userdemo/demo_gridspec02.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
def make_ticklabels_invisible(fig):
1212
for i, ax in enumerate(fig.axes):
1313
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
14-
for tl in ax.get_xticklabels() + ax.get_yticklabels():
15-
tl.set_visible(False)
14+
ax.tick_params(labelbottom=False, labelleft=False)
1615

1716

1817
fig = plt.figure()

examples/userdemo/demo_gridspec03.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
def make_ticklabels_invisible(fig):
1212
for i, ax in enumerate(fig.axes):
1313
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
14-
for tl in ax.get_xticklabels() + ax.get_yticklabels():
15-
tl.set_visible(False)
14+
ax.tick_params(labelbottom=False, labelleft=False)
1615

1716

1817
# demo 3 : gridspec with subplotpars set.

examples/userdemo/demo_gridspec04.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
def make_ticklabels_invisible(fig):
1212
for i, ax in enumerate(fig.axes):
1313
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
14-
for tl in ax.get_xticklabels() + ax.get_yticklabels():
15-
tl.set_visible(False)
14+
ax.tick_params(labelbottom=False, labelleft=False)
1615

1716

1817
# gridspec inside gridspec

examples/userdemo/demo_gridspec05.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
def make_ticklabels_invisible(fig):
1212
for i, ax in enumerate(fig.axes):
1313
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
14-
for tl in ax.get_xticklabels() + ax.get_yticklabels():
15-
tl.set_visible(False)
14+
ax.tick_params(labelbottom=False, labelleft=False)
1615

1716

1817
f = plt.figure()

lib/matplotlib/figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,14 +1173,14 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
11731173
if sharex in ["col", "all"]:
11741174
# turn off all but the bottom row
11751175
for ax in axarr[:-1, :].flat:
1176-
for label in ax.get_xticklabels():
1177-
label.set_visible(False)
1176+
ax.xaxis.set_tick_params(which='both',
1177+
labelbottom=False, labeltop=False)
11781178
ax.xaxis.offsetText.set_visible(False)
11791179
if sharey in ["row", "all"]:
11801180
# turn off all but the first column
11811181
for ax in axarr[:, 1:].flat:
1182-
for label in ax.get_yticklabels():
1183-
label.set_visible(False)
1182+
ax.yaxis.set_tick_params(which='both',
1183+
labelleft=False, labelright=False)
11841184
ax.yaxis.offsetText.set_visible(False)
11851185

11861186
if squeeze:

lib/matplotlib/tests/test_colorbar.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ def _colorbar_extension_shape(spacing):
5050
boundaries = values = norm.boundaries
5151
# Create a subplot.
5252
cax = fig.add_subplot(4, 1, i + 1)
53-
# Turn off text and ticks.
54-
for item in cax.get_xticklabels() + cax.get_yticklabels() +\
55-
cax.get_xticklines() + cax.get_yticklines():
56-
item.set_visible(False)
5753
# Generate the colorbar.
5854
cb = ColorbarBase(cax, cmap=cmap, norm=norm,
5955
boundaries=boundaries, values=values,
6056
extend=extension_type, extendrect=True,
6157
orientation='horizontal', spacing=spacing)
58+
# Turn off text and ticks.
59+
cax.tick_params(left=False, labelleft=False,
60+
bottom=False, labelbottom=False)
6261
# Return the figure to the caller.
6362
return fig
6463

@@ -82,15 +81,14 @@ def _colorbar_extension_length(spacing):
8281
for j, extendfrac in enumerate((None, 'auto', 0.1)):
8382
# Create a subplot.
8483
cax = fig.add_subplot(12, 1, i*3 + j + 1)
85-
# Turn off text and ticks.
86-
for item in cax.get_xticklabels() + cax.get_yticklabels() +\
87-
cax.get_xticklines() + cax.get_yticklines():
88-
item.set_visible(False)
8984
# Generate the colorbar.
9085
ColorbarBase(cax, cmap=cmap, norm=norm,
9186
boundaries=boundaries, values=values,
9287
extend=extension_type, extendfrac=extendfrac,
9388
orientation='horizontal', spacing=spacing)
89+
# Turn off text and ticks.
90+
cax.tick_params(left=False, labelleft=False,
91+
bottom=False, labelbottom=False)
9492
# Return the figure to the caller.
9593
return fig
9694

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ def test_cmap_and_norm_from_levels_and_colors():
287287
plt.colorbar(m)
288288

289289
# Hide the axes labels (but not the colorbar ones, as they are useful)
290-
for lab in ax.get_xticklabels() + ax.get_yticklabels():
291-
lab.set_visible(False)
290+
ax.tick_params(labelleft=False, labelbottom=False)
292291

293292

294293
def test_cmap_and_norm_from_levels_and_colors2():

lib/matplotlib/tests/test_figure.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,24 @@ def test_invalid_figure_size():
298298

299299
with pytest.raises(ValueError):
300300
fig.add_axes((.1, .1, .5, np.nan))
301+
302+
303+
def test_subplots_shareax_loglabels():
304+
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
305+
for ax in ax_arr.flatten():
306+
ax.plot([10, 20, 30], [10, 20, 30])
307+
308+
ax.set_yscale("log")
309+
ax.set_xscale("log")
310+
311+
for ax in ax_arr[0, :]:
312+
assert 0 == len(ax.xaxis.get_ticklabels(which='both'))
313+
314+
for ax in ax_arr[1, :]:
315+
assert 0 < len(ax.xaxis.get_ticklabels(which='both'))
316+
317+
for ax in ax_arr[:, 1]:
318+
assert 0 == len(ax.yaxis.get_ticklabels(which='both'))
319+
320+
for ax in ax_arr[:, 0]:
321+
assert 0 < len(ax.yaxis.get_ticklabels(which='both'))

lib/matplotlib/tests/test_patheffects.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ def test_patheffect1():
2323
foreground="w"),
2424
path_effects.Normal()])
2525

26-
ax1.grid(True, linestyle="-")
27-
2826
pe = [path_effects.withStroke(linewidth=3, foreground="w")]
29-
for l in ax1.get_xgridlines() + ax1.get_ygridlines():
30-
l.set_path_effects(pe)
27+
ax1.grid(True, linestyle="-", path_effects=pe)
3128

3229

3330
@image_comparison(baseline_images=['patheffect2'], remove_text=True)

tools/make_icons.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ def make_matplotlib_icon():
7979
for r, bar in zip(radii, bars):
8080
bar.set_facecolor(cm.jet(r/10.))
8181

82-
for label in ax.get_xticklabels() + ax.get_yticklabels():
83-
label.set_visible(False)
84-
85-
for line in ax.get_ygridlines() + ax.get_xgridlines():
86-
line.set_lw(0.0)
82+
ax.tick_params(labelleft=False, labelright=False,
83+
labelbottom=False, labeltop=False)
84+
ax.grid(lw=0.0)
8785

8886
ax.set_yticks(np.arange(1, 9, 2))
8987
ax.set_rmax(9)

0 commit comments

Comments
 (0)