@@ -46,6 +46,84 @@ statement, such as::
46
46
47
47
.. include:: ../../TODO
48
48
49
+ docstrings
50
+ ----------
51
+
52
+ In addition to the "narrative" documentation described above,
53
+ matplotlib also defines its API reference documentation in docstrings.
54
+ For the most part, these are standard Python docstrings, but
55
+ matplotlib also includes some features to better support documenting
56
+ getters and setters.
57
+
58
+ Matplotlib uses artist introspection of docstrings to support
59
+ properties. All properties that you want to support through ``setp ``
60
+ and ``getp `` should have a ``set_property `` and ``get_property ``
61
+ method in the :class: `~matplotlib.artist.Artist ` class. Yes, this is
62
+ not ideal given python properties or enthought traits, but it is a
63
+ historical legacy for now. The setter methods use the docstring with
64
+ the ACCEPTS token to indicate the type of argument the method accepts.
65
+ Eg. in :class: `matplotlib.lines.Line2D `::
66
+
67
+ # in lines.py
68
+ def set_linestyle(self, linestyle):
69
+ """
70
+ Set the linestyle of the line
71
+
72
+ ACCEPTS: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' | ' ' | '' ]
73
+ """
74
+
75
+ Since matplotlib uses a lot of pass-through ``kwargs ``, eg. in every
76
+ function that creates a line (:func: `~matplotlib.pyplot.plot `,
77
+ :func: `~matplotlib.pyplot.semilogx `,
78
+ :func: `~matplotlib.pyplot.semilogy `, etc...), it can be difficult for
79
+ the new user to know which ``kwargs `` are supported. Matplotlib uses
80
+ a docstring interpolation scheme to support documentation of every
81
+ function that takes a ``**kwargs ``. The requirements are:
82
+
83
+ 1. single point of configuration so changes to the properties don't
84
+ require multiple docstring edits.
85
+
86
+ 2. as automated as possible so that as properties change, the docs
87
+ are updated automagically.
88
+
89
+ The functions :attr: `matplotlib.artist.kwdocd ` and
90
+ :func: `matplotlib.artist.kwdoc ` to facilitate this. They combine
91
+ python string interpolation in the docstring with the matplotlib
92
+ artist introspection facility that underlies ``setp `` and ``getp ``.
93
+ The ``kwdocd `` is a single dictionary that maps class name to a
94
+ docstring of ``kwargs ``. Here is an example from
95
+ :mod: `matplotlib.lines `::
96
+
97
+ # in lines.py
98
+ artist.kwdocd['Line2D'] = artist.kwdoc(Line2D)
99
+
100
+ Then in any function accepting :class: `~matplotlib.lines.Line2D `
101
+ pass-through ``kwargs ``, eg. :meth: `matplotlib.axes.Axes.plot `::
102
+
103
+ # in axes.py
104
+ def plot(self, *args, **kwargs):
105
+ """
106
+ Some stuff omitted
107
+
108
+ The kwargs are Line2D properties:
109
+ %(Line2D)s
110
+
111
+ kwargs scalex and scaley, if defined, are passed on
112
+ to autoscale_view to determine whether the x and y axes are
113
+ autoscaled; default True. See Axes.autoscale_view for more
114
+ information
115
+ """
116
+ pass
117
+ plot.__doc__ = cbook.dedent(plot.__doc__) % artist.kwdocd
118
+
119
+ Note there is a problem for :class: `~matplotlib.artist.Artist `
120
+ ``__init__ `` methods, eg. :meth: `matplotlib.patches.Patch.__init__ `,
121
+ which supports ``Patch `` ``kwargs ``, since the artist inspector cannot
122
+ work until the class is fully defined and we can't modify the
123
+ ``Patch.__init__.__doc__ `` docstring outside the class definition.
124
+ There are some some manual hacks in this case, violating the
125
+ "single entry point" requirement above -- see the
126
+ ``artist.kwdocd['Patch'] `` setting in :mod: `matplotlib.patches `.
49
127
50
128
.. _formatting-mpl-docs :
51
129
@@ -181,11 +259,6 @@ working with Sphinx in general. Here are a few additional things to keep in mind
181
259
.. _`inline markup` : http://sphinx.pocoo.org/markup/inline.html
182
260
.. _index : http://sphinx.pocoo.org/markup/para.html#index-generating-markup
183
261
184
- Docstrings
185
- ----------
186
-
187
- In addition to the aforementioned formatting suggestions:
188
-
189
262
* Please limit the text width of docstrings to 70 characters.
190
263
191
264
* Keyword arguments should be described using a definition list.
@@ -195,7 +268,6 @@ In addition to the aforementioned formatting suggestions:
195
268
arguments, there are a many cases where a table is used in place of a
196
269
definition list for autogenerated sections of docstrings.
197
270
198
-
199
271
Figures
200
272
=======
201
273
0 commit comments