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

Skip to content

Commit 977ed43

Browse files
committed
Don't duplicate docstrings of pyplot-level cmap setters.
Dynamically generating these docstrings takes ~40us, which is negligible compared to the import time. flake8 had to be updated, as older versions incorrectly warned about the lack of blank lines between the one-liners.
1 parent 28f41f9 commit 977ed43

File tree

3 files changed

+36
-222
lines changed

3 files changed

+36
-222
lines changed

lib/matplotlib/pyplot.py

Lines changed: 31 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,11 +1978,10 @@ def colormaps():
19781978

19791979
def _setup_pyplot_info_docstrings():
19801980
"""
1981-
Generate the plotting docstring.
1981+
Setup the docstring of `plotting` and of the colormap-setting functions.
19821982
1983-
These must be done after the entire module is imported, so it is
1984-
called from the end of this module, which is generated by
1985-
boilerplate.py.
1983+
These must be done after the entire module is imported, so it is called
1984+
from the end of this module, which is generated by boilerplate.py.
19861985
"""
19871986
commands = get_plot_commands()
19881987

@@ -2018,6 +2017,15 @@ def _setup_pyplot_info_docstrings():
20182017
]
20192018
plotting.__doc__ = '\n'.join(lines)
20202019

2020+
for cm_name in colormaps():
2021+
if cm_name in globals():
2022+
globals()[cm_name].__doc__ = f"""
2023+
Set the colormap to {cm_name!r}.
2024+
2025+
This changes the default colormap as well as the colormap of the current
2026+
image if there is one. See ``help(colormaps)`` for more information.
2027+
"""
2028+
20212029

20222030
## Plotting part 1: manually generated functions and wrappers ##
20232031

@@ -2979,211 +2987,25 @@ def yscale(value, **kwargs):
29792987

29802988

29812989
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2982-
def autumn():
2983-
"""
2984-
Set the colormap to "autumn".
2985-
2986-
This changes the default colormap as well as the colormap of the current
2987-
image if there is one. See ``help(colormaps)`` for more information.
2988-
"""
2989-
set_cmap("autumn")
2990-
2991-
2992-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2993-
def bone():
2994-
"""
2995-
Set the colormap to "bone".
2996-
2997-
This changes the default colormap as well as the colormap of the current
2998-
image if there is one. See ``help(colormaps)`` for more information.
2999-
"""
3000-
set_cmap("bone")
2990+
def autumn(): set_cmap('autumn')
2991+
def bone(): set_cmap('bone')
2992+
def cool(): set_cmap('cool')
2993+
def copper(): set_cmap('copper')
2994+
def flag(): set_cmap('flag')
2995+
def gray(): set_cmap('gray')
2996+
def hot(): set_cmap('hot')
2997+
def hsv(): set_cmap('hsv')
2998+
def jet(): set_cmap('jet')
2999+
def pink(): set_cmap('pink')
3000+
def prism(): set_cmap('prism')
3001+
def spring(): set_cmap('spring')
3002+
def summer(): set_cmap('summer')
3003+
def winter(): set_cmap('winter')
3004+
def magma(): set_cmap('magma')
3005+
def inferno(): set_cmap('inferno')
3006+
def plasma(): set_cmap('plasma')
3007+
def viridis(): set_cmap('viridis')
3008+
def nipy_spectral(): set_cmap('nipy_spectral')
30013009

30023010

3003-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3004-
def cool():
3005-
"""
3006-
Set the colormap to "cool".
3007-
3008-
This changes the default colormap as well as the colormap of the current
3009-
image if there is one. See ``help(colormaps)`` for more information.
3010-
"""
3011-
set_cmap("cool")
3012-
3013-
3014-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3015-
def copper():
3016-
"""
3017-
Set the colormap to "copper".
3018-
3019-
This changes the default colormap as well as the colormap of the current
3020-
image if there is one. See ``help(colormaps)`` for more information.
3021-
"""
3022-
set_cmap("copper")
3023-
3024-
3025-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3026-
def flag():
3027-
"""
3028-
Set the colormap to "flag".
3029-
3030-
This changes the default colormap as well as the colormap of the current
3031-
image if there is one. See ``help(colormaps)`` for more information.
3032-
"""
3033-
set_cmap("flag")
3034-
3035-
3036-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3037-
def gray():
3038-
"""
3039-
Set the colormap to "gray".
3040-
3041-
This changes the default colormap as well as the colormap of the current
3042-
image if there is one. See ``help(colormaps)`` for more information.
3043-
"""
3044-
set_cmap("gray")
3045-
3046-
3047-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3048-
def hot():
3049-
"""
3050-
Set the colormap to "hot".
3051-
3052-
This changes the default colormap as well as the colormap of the current
3053-
image if there is one. See ``help(colormaps)`` for more information.
3054-
"""
3055-
set_cmap("hot")
3056-
3057-
3058-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3059-
def hsv():
3060-
"""
3061-
Set the colormap to "hsv".
3062-
3063-
This changes the default colormap as well as the colormap of the current
3064-
image if there is one. See ``help(colormaps)`` for more information.
3065-
"""
3066-
set_cmap("hsv")
3067-
3068-
3069-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3070-
def jet():
3071-
"""
3072-
Set the colormap to "jet".
3073-
3074-
This changes the default colormap as well as the colormap of the current
3075-
image if there is one. See ``help(colormaps)`` for more information.
3076-
"""
3077-
set_cmap("jet")
3078-
3079-
3080-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3081-
def pink():
3082-
"""
3083-
Set the colormap to "pink".
3084-
3085-
This changes the default colormap as well as the colormap of the current
3086-
image if there is one. See ``help(colormaps)`` for more information.
3087-
"""
3088-
set_cmap("pink")
3089-
3090-
3091-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3092-
def prism():
3093-
"""
3094-
Set the colormap to "prism".
3095-
3096-
This changes the default colormap as well as the colormap of the current
3097-
image if there is one. See ``help(colormaps)`` for more information.
3098-
"""
3099-
set_cmap("prism")
3100-
3101-
3102-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3103-
def spring():
3104-
"""
3105-
Set the colormap to "spring".
3106-
3107-
This changes the default colormap as well as the colormap of the current
3108-
image if there is one. See ``help(colormaps)`` for more information.
3109-
"""
3110-
set_cmap("spring")
3111-
3112-
3113-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3114-
def summer():
3115-
"""
3116-
Set the colormap to "summer".
3117-
3118-
This changes the default colormap as well as the colormap of the current
3119-
image if there is one. See ``help(colormaps)`` for more information.
3120-
"""
3121-
set_cmap("summer")
3122-
3123-
3124-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3125-
def winter():
3126-
"""
3127-
Set the colormap to "winter".
3128-
3129-
This changes the default colormap as well as the colormap of the current
3130-
image if there is one. See ``help(colormaps)`` for more information.
3131-
"""
3132-
set_cmap("winter")
3133-
3134-
3135-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3136-
def magma():
3137-
"""
3138-
Set the colormap to "magma".
3139-
3140-
This changes the default colormap as well as the colormap of the current
3141-
image if there is one. See ``help(colormaps)`` for more information.
3142-
"""
3143-
set_cmap("magma")
3144-
3145-
3146-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3147-
def inferno():
3148-
"""
3149-
Set the colormap to "inferno".
3150-
3151-
This changes the default colormap as well as the colormap of the current
3152-
image if there is one. See ``help(colormaps)`` for more information.
3153-
"""
3154-
set_cmap("inferno")
3155-
3156-
3157-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3158-
def plasma():
3159-
"""
3160-
Set the colormap to "plasma".
3161-
3162-
This changes the default colormap as well as the colormap of the current
3163-
image if there is one. See ``help(colormaps)`` for more information.
3164-
"""
3165-
set_cmap("plasma")
3166-
3167-
3168-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3169-
def viridis():
3170-
"""
3171-
Set the colormap to "viridis".
3172-
3173-
This changes the default colormap as well as the colormap of the current
3174-
image if there is one. See ``help(colormaps)`` for more information.
3175-
"""
3176-
set_cmap("viridis")
3177-
3178-
3179-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3180-
def nipy_spectral():
3181-
"""
3182-
Set the colormap to "nipy_spectral".
3183-
3184-
This changes the default colormap as well as the colormap of the current
3185-
image if there is one. See ``help(colormaps)`` for more information.
3186-
"""
3187-
set_cmap("nipy_spectral")
3188-
31893011
_setup_pyplot_info_docstrings()

requirements/testing/flake8.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Extra pip requirements for the GitHub Actions flake8 build
22

3-
flake8>=3.7
3+
flake8>=3.8
44
pydocstyle<4.0
55
flake8-docstrings

tools/boilerplate.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,7 @@ def {name}{signature}:
5757
return gcf().{called_name}{call}
5858
"""
5959

60-
# Used for colormap functions
61-
CMAP_TEMPLATE = AUTOGEN_MSG + '''
62-
def {name}():
63-
"""
64-
Set the colormap to "{name}".
65-
66-
This changes the default colormap as well as the colormap of the current
67-
image if there is one. See ``help(colormaps)`` for more information.
68-
"""
69-
set_cmap("{name}")
70-
'''
60+
CMAP_TEMPLATE = "def {name}(): set_cmap({name!r})\n" # Colormap functions.
7161

7262

7363
class value_formatter:
@@ -310,6 +300,8 @@ def boilerplate_gen():
310300
yield generate_function(name, f'Axes.{called_name}', template,
311301
sci_command=cmappable.get(name))
312302

303+
yield AUTOGEN_MSG
304+
yield '\n'
313305
cmaps = (
314306
'autumn',
315307
'bone',
@@ -335,7 +327,7 @@ def boilerplate_gen():
335327
for name in cmaps:
336328
yield CMAP_TEMPLATE.format(name=name)
337329

338-
yield '\n'
330+
yield '\n\n'
339331
yield '_setup_pyplot_info_docstrings()'
340332

341333

0 commit comments

Comments
 (0)