Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ac69b10

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

File tree

Expand file treeCollapse file tree

17 files changed

+413
-133
lines changed
Filter options
Expand file treeCollapse file tree

17 files changed

+413
-133
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ per-file-ignores =
180180
examples/misc/svg_filter_line.py: E402
181181
examples/misc/svg_filter_pie.py: E402
182182
examples/misc/table_demo.py: E201
183+
examples/mplot3d/surface3d.py: E402
183184
examples/pie_and_polar_charts/bar_of_pie.py: E402
184185
examples/pie_and_polar_charts/nested_pie.py: E402
185186
examples/pie_and_polar_charts/pie_and_donut_labels.py: E402
@@ -232,6 +233,8 @@ per-file-ignores =
232233
examples/shapes_and_collections/path_patch.py: E402
233234
examples/shapes_and_collections/quad_bezier.py: E402
234235
examples/shapes_and_collections/scatter.py: E402
236+
examples/showcase/anatomy.py: E402
237+
examples/showcase/bachelors_degrees_by_gender.py: E402
235238
examples/showcase/firefox.py: E501
236239
examples/specialty_plots/anscombe.py: E402
237240
examples/specialty_plots/radar_chart.py: E402
@@ -251,6 +254,7 @@ per-file-ignores =
251254
examples/subplots_axes_and_figures/two_scales.py: E402
252255
examples/subplots_axes_and_figures/zoom_inset_axes.py: E402
253256
examples/tests/backend_driver_sgskip.py: E402
257+
examples/text_labels_and_annotations/date_index_formatter.py: E402
254258
examples/text_labels_and_annotations/demo_text_rotation_mode.py: E402
255259
examples/text_labels_and_annotations/custom_legends.py: E402
256260
examples/text_labels_and_annotations/fancyarrow_demo.py: E402
@@ -261,7 +265,11 @@ per-file-ignores =
261265
examples/text_labels_and_annotations/mathtext_asarray.py: E402
262266
examples/text_labels_and_annotations/tex_demo.py: E402
263267
examples/text_labels_and_annotations/watermark_text.py: E402
268+
examples/ticks_and_spines/custom_ticker1.py: E402
264269
examples/ticks_and_spines/date_concise_formatter.py: E402
270+
examples/ticks_and_spines/major_minor_demo.py: E402
271+
examples/ticks_and_spines/tick-formatters.py: E402
272+
examples/ticks_and_spines/tick_labels_from_values.py: E402
265273
examples/user_interfaces/canvasagg.py: E402
266274
examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py: E402
267275
examples/user_interfaces/embedding_in_gtk3_sgskip.py: E402
+7Lines 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

Copy file name to clipboardExpand all lines: examples/mplot3d/surface3d.py
+21-2Lines 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

Copy file name to clipboardExpand all lines: examples/pyplots/dollar_ticks.py
+8-10Lines 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

Copy file name to clipboardExpand all lines: examples/showcase/anatomy.py
+25-3Lines 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

Copy file name to clipboardExpand all lines: examples/showcase/bachelors_degrees_by_gender.py
+22-2Lines 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

Copy file name to clipboardExpand all lines: examples/text_labels_and_annotations/date_index_formatter.py
+19-2Lines 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

Copy file name to clipboardExpand all lines: examples/ticks_and_spines/custom_ticker1.py
+19-10Lines 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

Copy file name to clipboardExpand all lines: examples/ticks_and_spines/major_minor_demo.py
+31-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.