Automatically create tick formatters for str and callable inputs. · matplotlib/matplotlib@ac69b10 · GitHub
Skip to content

Commit ac69b10

Browse files
committed
Automatically create tick formatters for str and callable inputs.
1 parent 480ea3f commit ac69b10

17 files changed

Lines changed: 413 additions & 133 deletions

File tree

.flake8

Lines changed: 8 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Allow tick formatters to be set with str or function inputs
2+
------------------------------------------------------------------------
3+
`~.Axis.set_major_formatter` and `~.Axis.set_minor_formatter`
4+
now accept `str` or function inputs in addition to `~.ticker.Formatter`
5+
instances. For a `str` a `~.ticker.StrMethodFormatter` is automatically
6+
generated and used. For a function a `~.ticker.FuncFormatter` is automatically
7+
generated and used.

examples/mplot3d/surface3d.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import matplotlib.pyplot as plt
1414
from matplotlib import cm
15-
from matplotlib.ticker import LinearLocator, FormatStrFormatter
15+
from matplotlib.ticker import LinearLocator
1616
import numpy as np
1717

1818
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
@@ -31,9 +31,28 @@
3131
# Customize the z axis.
3232
ax.set_zlim(-1.01, 1.01)
3333
ax.zaxis.set_major_locator(LinearLocator(10))
34-
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
34+
# A StrMethodFormatter is used automatically
35+
ax.zaxis.set_major_formatter('{x:.02f}')
3536

3637
# Add a color bar which maps values to colors.
3738
fig.colorbar(surf, shrink=0.5, aspect=5)
3839

3940
plt.show()
41+
42+
43+
#############################################################################
44+
#
45+
# ------------
46+
#
47+
# References
48+
# """"""""""
49+
#
50+
# The use of the following functions, methods, classes and modules is shown
51+
# in this example:
52+
53+
import matplotlib
54+
matplotlib.pyplot.subplots
55+
matplotlib.axis.Axis.set_major_formatter
56+
matplotlib.axis.Axis.set_major_locator
57+
matplotlib.ticker.LinearLocator
58+
matplotlib.ticker.StrMethodFormatter

examples/pyplots/dollar_ticks.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@
77
"""
88
import numpy as np
99
import matplotlib.pyplot as plt
10-
import matplotlib.ticker as ticker
1110

1211
# Fixing random state for reproducibility
1312
np.random.seed(19680801)
1413

1514
fig, ax = plt.subplots()
1615
ax.plot(100*np.random.rand(20))
1716

18-
formatter = ticker.FormatStrFormatter('$%1.2f')
19-
ax.yaxis.set_major_formatter(formatter)
17+
# Use automatic StrMethodFormatter
18+
ax.yaxis.set_major_formatter('${x:1.2f}')
2019

21-
for tick in ax.yaxis.get_major_ticks():
22-
tick.label1.set_visible(False)
23-
tick.label2.set_visible(True)
24-
tick.label2.set_color('green')
20+
ax.yaxis.set_tick_params(which='major', labelcolor='green',
21+
labelleft=False, labelright=True)
2522

2623
plt.show()
2724

25+
2826
#############################################################################
2927
#
3028
# ------------
@@ -36,8 +34,8 @@
3634
# in this example:
3735

3836
import matplotlib
39-
matplotlib.ticker
40-
matplotlib.ticker.FormatStrFormatter
37+
matplotlib.pyplot.subplots
4138
matplotlib.axis.Axis.set_major_formatter
42-
matplotlib.axis.Axis.get_major_ticks
39+
matplotlib.axis.Axis.set_tick_params
4340
matplotlib.axis.Tick
41+
matplotlib.ticker.StrMethodFormatter

examples/showcase/anatomy.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010
import matplotlib.pyplot as plt
11-
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
11+
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
1212

1313
np.random.seed(19680801)
1414

@@ -24,13 +24,14 @@
2424
def minor_tick(x, pos):
2525
if not x % 1.0:
2626
return ""
27-
return "%.2f" % x
27+
return f"{x:.2f}"
2828

2929
ax.xaxis.set_major_locator(MultipleLocator(1.000))
3030
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
3131
ax.yaxis.set_major_locator(MultipleLocator(1.000))
3232
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
33-
ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))
33+
# FuncFormatter is created and used automatically
34+
ax.xaxis.set_minor_formatter(minor_tick)
3435

3536
ax.set_xlim(0, 4)
3637
ax.set_ylim(0, 4)
@@ -141,3 +142,24 @@ def text(x, y, text):
141142
fontsize=10, ha="right", color='.5')
142143

143144
plt.show()
145+
146+
147+
#############################################################################
148+
#
149+
# ------------
150+
#
151+
# References
152+
# """"""""""
153+
#
154+
# The use of the following functions, methods, classes and modules is shown
155+
# in this example:
156+
157+
import matplotlib
158+
matplotlib.pyplot.figure
159+
matplotlib.axes.Axes.text
160+
matplotlib.axis.Axis.set_minor_formatter
161+
matplotlib.axis.Axis.set_major_locator
162+
matplotlib.axis.Axis.set_minor_locator
163+
matplotlib.patches.Circle
164+
matplotlib.patheffects.withStroke
165+
matplotlib.ticker.FuncFormatter

examples/showcase/bachelors_degrees_by_gender.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
# Set a fixed location and format for ticks.
5252
ax.set_xticks(range(1970, 2011, 10))
5353
ax.set_yticks(range(0, 91, 10))
54-
ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
55-
ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format))
54+
# Use automatic StrMethodFormatter creation
55+
ax.xaxis.set_major_formatter('{x:.0f}')
56+
ax.yaxis.set_major_formatter('{x:.0f}%')
5657

5758
# Provide tick lines across the plot to help your viewers trace along
5859
# the axis ticks. Make sure that the lines are light and small so they
@@ -113,3 +114,22 @@
113114
# Just change the file extension in this call.
114115
# fig.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
115116
plt.show()
117+
118+
#############################################################################
119+
#
120+
# ------------
121+
#
122+
# References
123+
# """"""""""
124+
#
125+
# The use of the following functions, methods, classes and modules is shown
126+
# in this example:
127+
128+
import matplotlib
129+
matplotlib.pyplot.subplots
130+
matplotlib.axes.Axes.text
131+
matplotlib.axis.Axis.set_major_formatter
132+
matplotlib.axis.XAxis.tick_bottom
133+
matplotlib.axis.YAxis.tick_left
134+
matplotlib.artist.Artist.set_visible
135+
matplotlib.ticker.StrMethodFormatter

examples/text_labels_and_annotations/date_index_formatter.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111
import matplotlib.pyplot as plt
1212
import matplotlib.cbook as cbook
13-
import matplotlib.ticker as ticker
1413

1514
# Load a numpy record array from yahoo csv data with fields date, open, close,
1615
# volume, adj_close from the mpl-data/example directory. The record array
@@ -36,8 +35,26 @@ def format_date(x, pos=None):
3635

3736

3837
ax2.plot(ind, r.adj_close, 'o-')
39-
ax2.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
38+
# Use automatic FuncFormatter creation
39+
ax2.xaxis.set_major_formatter(format_date)
4040
ax2.set_title("Custom tick formatter")
4141
fig.autofmt_xdate()
4242

4343
plt.show()
44+
45+
46+
#############################################################################
47+
#
48+
# ------------
49+
#
50+
# References
51+
# """"""""""
52+
#
53+
# The use of the following functions, methods, classes and modules is shown
54+
# in this example:
55+
56+
import matplotlib
57+
matplotlib.pyplot.subplots
58+
matplotlib.axis.Axis.set_major_formatter
59+
matplotlib.cbook.get_sample_data
60+
matplotlib.ticker.FuncFormatter

examples/ticks_and_spines/custom_ticker1.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@
1111
In this example a user defined function is used to format the ticks in
1212
millions of dollars on the y axis.
1313
"""
14-
from matplotlib.ticker import FuncFormatter
1514
import matplotlib.pyplot as plt
16-
import numpy as np
1715

18-
x = np.arange(4)
1916
money = [1.5e5, 2.5e6, 5.5e6, 2.0e7]
2017

2118

2219
def millions(x, pos):
2320
"""The two args are the value and tick position."""
24-
return '$%1.1fM' % (x * 1e-6)
25-
26-
27-
formatter = FuncFormatter(millions)
21+
return '${:1.1f}M'.format(x*1e-6)
2822

2923
fig, ax = plt.subplots()
30-
ax.yaxis.set_major_formatter(formatter)
31-
plt.bar(x, money)
32-
plt.xticks(x, ('Bill', 'Fred', 'Mary', 'Sue'))
24+
# Use automatic FuncFormatter creation
25+
ax.yaxis.set_major_formatter(millions)
26+
ax.bar(['Bill', 'Fred', 'Mary', 'Sue'], money)
3327
plt.show()
28+
29+
#############################################################################
30+
#
31+
# ------------
32+
#
33+
# References
34+
# """"""""""
35+
#
36+
# The use of the following functions, methods, classes and modules is shown
37+
# in this example:
38+
39+
import matplotlib
40+
matplotlib.pyplot.subplots
41+
matplotlib.axis.Axis.set_major_formatter
42+
matplotlib.ticker.FuncFormatter

examples/ticks_and_spines/major_minor_demo.py

Lines changed: 31 additions & 7 deletions

0 commit comments

Comments
 (0)