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

0 commit comments

Comments
 (0)