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

Skip to content

Commit 763ef98

Browse files
committed
Automatically create tick formatters for str and callable inputs.
1 parent 47a15d9 commit 763ef98

File tree

17 files changed

+385
-102
lines changed

17 files changed

+385
-102
lines changed

.flake8

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ per-file-ignores =
168168
examples/misc/svg_filter_line.py: E402
169169
examples/misc/svg_filter_pie.py: E402
170170
examples/misc/table_demo.py: E201
171+
examples/mplot3d/surface3d.py: E402
171172
examples/pie_and_polar_charts/bar_of_pie.py: E402
172173
examples/pie_and_polar_charts/nested_pie.py: E402
173174
examples/pie_and_polar_charts/pie_and_donut_labels.py: E402
@@ -220,6 +221,8 @@ per-file-ignores =
220221
examples/shapes_and_collections/path_patch.py: E402
221222
examples/shapes_and_collections/quad_bezier.py: E402
222223
examples/shapes_and_collections/scatter.py: E402
224+
examples/showcase/anatomy.py: E402
225+
examples/showcase/bachelors_degrees_by_gender.py: E402
223226
examples/showcase/firefox.py: E501
224227
examples/specialty_plots/anscombe.py: E402
225228
examples/specialty_plots/radar_chart.py: E402
@@ -239,6 +242,7 @@ per-file-ignores =
239242
examples/subplots_axes_and_figures/two_scales.py: E402
240243
examples/subplots_axes_and_figures/zoom_inset_axes.py: E402
241244
examples/tests/backend_driver_sgskip.py: E402
245+
examples/text_labels_and_annotations/date_index_formatter.py: E402
242246
examples/text_labels_and_annotations/demo_text_rotation_mode.py: E402
243247
examples/text_labels_and_annotations/custom_legends.py: E402
244248
examples/text_labels_and_annotations/fancyarrow_demo.py: E402
@@ -249,7 +253,11 @@ per-file-ignores =
249253
examples/text_labels_and_annotations/mathtext_asarray.py: E402
250254
examples/text_labels_and_annotations/tex_demo.py: E402
251255
examples/text_labels_and_annotations/watermark_text.py: E402
256+
examples/ticks_and_spines/custom_ticker1.py: E402
252257
examples/ticks_and_spines/date_concise_formatter.py: E402
258+
examples/ticks_and_spines/major_minor_demo.py: E402
259+
examples/ticks_and_spines/tick-formatters.py: E402
260+
examples/ticks_and_spines/tick_labels_from_values.py: E402
253261
examples/user_interfaces/canvasagg.py: E402
254262
examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402
255263
examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402
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+
created and used. For a function a `~.ticker.FuncFormatter` is automatically
7+
created and used.

examples/mplot3d/surface3d.py

Lines changed: 20 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,27 @@
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.StrMethodFormatter

examples/pyplots/dollar_ticks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
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

2120
for tick in ax.yaxis.get_major_ticks():
2221
tick.label1.set_visible(False)
@@ -25,6 +24,7 @@
2524

2625
plt.show()
2726

27+
2828
#############################################################################
2929
#
3030
# ------------
@@ -36,8 +36,8 @@
3636
# in this example:
3737

3838
import matplotlib
39-
matplotlib.ticker
40-
matplotlib.ticker.FormatStrFormatter
39+
matplotlib.pyplot.subplots
4140
matplotlib.axis.Axis.set_major_formatter
4241
matplotlib.axis.Axis.get_major_ticks
4342
matplotlib.axis.Tick
43+
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: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
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
1615
import numpy as np
1716

@@ -21,13 +20,26 @@
2120

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

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

examples/ticks_and_spines/major_minor_demo.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
Minor tick labels can be turned on by setting the minor formatter.
1515
1616
`.MultipleLocator` places ticks on multiples of some base.
17-
`.FormatStrFormatter` uses a format string (e.g., ``'%d'`` or ``'%1.2f'`` or
18-
``'%1.1f cm'``) to format the tick labels.
17+
`.StrMethodFormatter` uses a format string (e.g., ``'{x:d}'`` or ``'{x:1.2f}'``
18+
or ``'{x:1.1f} cm'``) to format the tick labels (the variable in the format
19+
string must be ``'x'``). For a `.StrMethodFormatter`, the string can be passed
20+
directly to `.Axis.set_major_formatter` or
21+
`.Axis.set_minor_formatter`. An appropriate `.StrMethodFormatter` will
22+
be created and used automatically.
1923
2024
`.pyplot.grid` changes the grid settings of the major ticks of the y and y axis
2125
together. If you want to control the grid of the minor ticks for a given axis,
@@ -29,8 +33,7 @@
2933

3034
import matplotlib.pyplot as plt
3135
import numpy as np
32-
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
33-
AutoMinorLocator)
36+
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)
3437

3538

3639
t = np.arange(0.0, 100.0, 0.1)
@@ -40,10 +43,11 @@
4043
ax.plot(t, s)
4144

4245
# Make a plot with major ticks that are multiples of 20 and minor ticks that
43-
# are multiples of 5. Label major ticks with '%d' formatting but don't label
44-
# minor ticks.
46+
# are multiples of 5. Label major ticks with '.0f' formatting but don't label
47+
# minor ticks. The string is used directly, the `StrMethodFormatter` is
48+
# created automatically.
4549
ax.xaxis.set_major_locator(MultipleLocator(20))
46-
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
50+
ax.xaxis.set_major_formatter('{x:.0f}')
4751

4852
# For the minor ticks, use no labels; default NullFormatter.
4953
ax.xaxis.set_minor_locator(MultipleLocator(5))
@@ -74,3 +78,23 @@
7478
ax.tick_params(which='minor', length=4, color='r')
7579

7680
plt.show()
81+
82+
83+
#############################################################################
84+
#
85+
# ------------
86+
#
87+
# References
88+
# """"""""""
89+
#
90+
# The use of the following functions, methods, classes and modules is shown
91+
# in this example:
92+
93+
import matplotlib
94+
matplotlib.pyplot.subplots
95+
matplotlib.axis.Axis.set_major_formatter
96+
matplotlib.axis.Axis.set_major_locator
97+
matplotlib.axis.Axis.set_minor_locator
98+
matplotlib.ticker.AutoMinorLocator
99+
matplotlib.ticker.MultipleLocator
100+
matplotlib.ticker.StrMethodFormatter

0 commit comments

Comments
 (0)