Skip to content

Commit 2abeda1

Browse files
authored
Merge pull request #10190 from timhoffm/axes-doc-plot
DOC: improve docstring of Axes.plot
2 parents 0211860 + 1cefa87 commit 2abeda1

File tree

1 file changed

+206
-106
lines changed

1 file changed

+206
-106
lines changed

lib/matplotlib/axes/_axes.py

+206-106
Original file line numberDiff line numberDiff line change
@@ -1288,132 +1288,232 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
12881288
@docstring.dedent_interpd
12891289
def plot(self, *args, **kwargs):
12901290
"""
1291-
Plot lines and/or markers to the
1292-
:class:`~matplotlib.axes.Axes`. *args* is a variable length
1293-
argument, allowing for multiple *x*, *y* pairs with an
1294-
optional format string. For example, each of the following is
1295-
legal::
1291+
Plot y versus x as lines and/or markers.
12961292
1297-
plot(x, y) # plot x and y using default line style and color
1298-
plot(x, y, 'bo') # plot x and y using blue circle markers
1299-
plot(y) # plot y using x as index array 0..N-1
1300-
plot(y, 'r+') # ditto, but with red plusses
1293+
Call signatures::
1294+
1295+
plot([x], y, [fmt], data=None, **kwargs)
1296+
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
1297+
1298+
The coordinates of the points or line nodes are given by *x*, *y*.
1299+
1300+
The optional parameter *fmt* is a convenient way for defining basic
1301+
formatting like color, marker and linestyle. It's a shortcut string
1302+
notation described in the *Notes* section below.
1303+
1304+
>>> plot(x, y) # plot x and y using default line style and color
1305+
>>> plot(x, y, 'bo') # plot x and y using blue circle markers
1306+
>>> plot(y) # plot y using x as index array 0..N-1
1307+
>>> plot(y, 'r+') # ditto, but with red plusses
1308+
1309+
You can use `~.Line2D` properties as keyword arguments for more
1310+
control on the appearance. Line properties and *fmt* can be mixed.
1311+
The following two calls yield identical results:
1312+
1313+
>>> plot(x, y, 'go--', linewidth=2, markersize=12)
1314+
>>> plot(x, y, color='green', marker='o', linestyle='dashed', \
1315+
linewidth=2, markersize=12)
1316+
1317+
When conflicting with *fmt*, the *\*\*kwargs* properties are taking
1318+
precedence.
1319+
1320+
1321+
**Plotting labelled data**
1322+
1323+
There's a convenient way for plotting objects with labelled data (i.e.
1324+
data that can be accessed by index ``obj['y']``). Instead of giving
1325+
the data in *x* and *y*, you can provide the object in the *data*
1326+
parameter and just give the labels for *x* and *y*::
1327+
1328+
>>> plot('xlabel', 'ylabel', data=obj)
1329+
1330+
All indexable objects are supported. This could e.g. be a `dict`, a
1331+
`pandas.DataFame` or a structured numpy array.
13011332
1302-
If *x* and/or *y* is 2-dimensional, then the corresponding columns
1303-
will be plotted.
13041333
1305-
If used with labeled data, make sure that the color spec is not
1306-
included as an element in data, as otherwise the last case
1307-
``plot("v","r", data={"v":..., "r":...)``
1308-
can be interpreted as the first case which would do ``plot(v, r)``
1309-
using the default line style and color.
1334+
**Plotting multiple sets of data**
13101335
1311-
If not used with labeled data (i.e., without a data argument),
1312-
an arbitrary number of *x*, *y*, *fmt* groups can be specified, as in::
1336+
There are various ways to plot multiple sets of data.
13131337
1314-
a.plot(x1, y1, 'g^', x2, y2, 'g-')
1338+
- The most straight forward way is just to call `plot` multiple times.
1339+
Example:
13151340
1316-
Return value is a list of lines that were added.
1341+
>>> plot(x1, y1, 'bo')
1342+
>>> plot(x2, y2, 'go')
1343+
1344+
- Alternatively, if your data is already a 2d array, you can pass it
1345+
directly to *x*, *y*. A separate data set will be drawn for every
1346+
column.
1347+
1348+
Example: an array ``a`` where the first column represents the *x*
1349+
values and the other columns are the *y* columns::
1350+
1351+
>>> plot(a[0], a[1:])
1352+
1353+
- The third way is to specify multiple sets of *[x]*, *y*, *[fmt]*
1354+
groups::
1355+
1356+
>>> plot(x1, y1, 'g^', x2, y2, 'g-')
1357+
1358+
In this case, any additional keyword argument applies to all
1359+
datasets. Also this syntax cannot be combined with the *data*
1360+
parameter.
13171361
13181362
By default, each line is assigned a different style specified by a
1319-
'style cycle'. To change this behavior, you can edit the
1320-
axes.prop_cycle rcParam.
1321-
1322-
The following format string characters are accepted to control
1323-
the line style or marker:
1324-
1325-
================ ===============================
1326-
character description
1327-
================ ===============================
1328-
``'-'`` solid line style
1329-
``'--'`` dashed line style
1330-
``'-.'`` dash-dot line style
1331-
``':'`` dotted line style
1332-
``'.'`` point marker
1333-
``','`` pixel marker
1334-
``'o'`` circle marker
1335-
``'v'`` triangle_down marker
1336-
``'^'`` triangle_up marker
1337-
``'<'`` triangle_left marker
1338-
``'>'`` triangle_right marker
1339-
``'1'`` tri_down marker
1340-
``'2'`` tri_up marker
1341-
``'3'`` tri_left marker
1342-
``'4'`` tri_right marker
1343-
``'s'`` square marker
1344-
``'p'`` pentagon marker
1345-
``'*'`` star marker
1346-
``'h'`` hexagon1 marker
1347-
``'H'`` hexagon2 marker
1348-
``'+'`` plus marker
1349-
``'x'`` x marker
1350-
``'D'`` diamond marker
1351-
``'d'`` thin_diamond marker
1352-
``'|'`` vline marker
1353-
``'_'`` hline marker
1354-
================ ===============================
1363+
'style cycle'. The *fmt* and line property parameters are only
1364+
neccessary if you want explicit deviations from these defaults.
1365+
Alternatively, you can also change the style cycle using the
1366+
'axes.prop_cycle' rcParam.
13551367
1368+
Parameters
1369+
----------
1370+
x, y : array-like or scalar
1371+
The horizontal / vertical coordinates of the data points.
1372+
*x* values are optional. If not given, they default to
1373+
``[0, ..., N-1]``.
13561374
1357-
The following color abbreviations are supported:
1375+
Commonly, these parameters are arrays of length N. However,
1376+
scalars are supported as well (equivalent to an array with
1377+
constant value).
13581378
1359-
========== ========
1360-
character color
1361-
========== ========
1362-
'b' blue
1363-
'g' green
1364-
'r' red
1365-
'c' cyan
1366-
'm' magenta
1367-
'y' yellow
1368-
'k' black
1369-
'w' white
1370-
========== ========
1371-
1372-
In addition, you can specify colors in many weird and
1373-
wonderful ways, including full names (``'green'``), hex
1374-
strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
1375-
grayscale intensities as a string (``'0.8'``). Of these, the
1376-
string specifications can be used in place of a ``fmt`` group,
1377-
but the tuple forms can be used only as ``kwargs``.
1378-
1379-
Line styles and colors are combined in a single format string, as in
1380-
``'bo'`` for blue circles.
1381-
1382-
The *kwargs* can be used to set line properties (any property that has
1383-
a ``set_*`` method). You can use this to set a line label (for auto
1384-
legends), linewidth, antialiasing, marker face color, etc. Here is an
1385-
example::
1386-
1387-
plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
1388-
plot([1,2,3], [1,4,9], 'rs', label='line 2')
1389-
axis([0, 4, 0, 10])
1390-
legend()
1379+
The parameters can also be 2-dimensional. Then, the columns
1380+
represent separate data sets.
1381+
1382+
fmt : str, optional
1383+
A format string, e.g. 'ro' for red circles. See the *Notes*
1384+
section for a full description of the format strings.
1385+
1386+
Format strings are just an abbreviation for quickly setting
1387+
basic line properties. All of these and more can also be
1388+
controlled by keyword arguments.
13911389
1392-
If you make multiple lines with one plot command, the kwargs
1393-
apply to all those lines, e.g.::
1390+
data : indexable object, optional
1391+
An object with labelled data. If given, provide the label names to
1392+
plot in *x* and *y*.
13941393
1395-
plot(x1, y1, x2, y2, antialiased=False)
1394+
.. note::
1395+
Technically there's a slight ambiguity in calls where the
1396+
second label is a valid *fmt*. `plot('n', 'o', data=obj)`
1397+
could be `plt(x, y)` or `plt(y, fmt)`. In such cases,
1398+
the former interpretation is chosen, but a warning is issued.
1399+
You may suppress the warning by adding an empty format string
1400+
`plot('n', 'o', '', data=obj)`.
13961401
1397-
Neither line will be antialiased.
13981402
1399-
You do not need to use format strings, which are just
1400-
abbreviations. All of the line properties can be controlled
1401-
by keyword arguments. For example, you can set the color,
1402-
marker, linestyle, and markercolor with::
1403+
Other Parameters
1404+
----------------
1405+
scalex, scaley : bool, optional, default: True
1406+
These parameters determined if the view limits are adapted to
1407+
the data limits. The values are passed on to `autoscale_view`.
14031408
1404-
plot(x, y, color='green', linestyle='dashed', marker='o',
1405-
markerfacecolor='blue', markersize=12).
1409+
**kwargs : `~.Line2D` properties, optional
1410+
*kwargs* are used to specify properties like a line label (for
1411+
auto legends), linewidth, antialiasing, marker face color.
1412+
Example::
14061413
1407-
See :class:`~matplotlib.lines.Line2D` for details.
1414+
>>> plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
1415+
>>> plot([1,2,3], [1,4,9], 'rs', label='line 2')
14081416
1409-
The kwargs are :class:`~matplotlib.lines.Line2D` properties:
1417+
If you make multiple lines with one plot command, the kwargs
1418+
apply to all those lines.
14101419
1411-
%(Line2D)s
1420+
Here is a list of available `~.Line2D` properties:
1421+
1422+
%(Line2D)s
1423+
1424+
Returns
1425+
-------
1426+
lines
1427+
A list of `~.Line2D` objects that were added.
1428+
1429+
1430+
See Also
1431+
--------
1432+
scatter : XY scatter plot with markers of variing size and/or color (
1433+
sometimes also called bubble chart).
1434+
1435+
1436+
Notes
1437+
-----
1438+
**Format Strings**
1439+
1440+
A format string consists of a part for color, marker and line::
1441+
1442+
fmt = '[color][marker][line]'
1443+
1444+
Each of them is optional. If not provided, the value from the style
1445+
cycle is used. Exception: If ``line`` is given, but no ``marker``,
1446+
the data will be a line without markers.
1447+
1448+
**Colors**
1449+
1450+
The following color abbreviations are supported:
1451+
1452+
============= ===============================
1453+
character color
1454+
============= ===============================
1455+
``'b'`` blue
1456+
``'g'`` green
1457+
``'r'`` red
1458+
``'c'`` cyan
1459+
``'m'`` magenta
1460+
``'y'`` yellow
1461+
``'k'`` black
1462+
``'w'`` white
1463+
============= ===============================
1464+
1465+
If the color is the only part of the format string, you can
1466+
additionally use any `matplotlib.colors` spec, e.g. full names
1467+
(``'green'``) or hex strings (``'#008000'``).
1468+
1469+
**Markers**
1470+
1471+
============= ===============================
1472+
character description
1473+
============= ===============================
1474+
``'.'`` point marker
1475+
``','`` pixel marker
1476+
``'o'`` circle marker
1477+
``'v'`` triangle_down marker
1478+
``'^'`` triangle_up marker
1479+
``'<'`` triangle_left marker
1480+
``'>'`` triangle_right marker
1481+
``'1'`` tri_down marker
1482+
``'2'`` tri_up marker
1483+
``'3'`` tri_left marker
1484+
``'4'`` tri_right marker
1485+
``'s'`` square marker
1486+
``'p'`` pentagon marker
1487+
``'*'`` star marker
1488+
``'h'`` hexagon1 marker
1489+
``'H'`` hexagon2 marker
1490+
``'+'`` plus marker
1491+
``'x'`` x marker
1492+
``'D'`` diamond marker
1493+
``'d'`` thin_diamond marker
1494+
``'|'`` vline marker
1495+
``'_'`` hline marker
1496+
============= ===============================
1497+
1498+
**Line Styles**
1499+
1500+
============= ===============================
1501+
character description
1502+
============= ===============================
1503+
``'-'`` solid line style
1504+
``'--'`` dashed line style
1505+
``'-.'`` dash-dot line style
1506+
``':'`` dotted line style
1507+
============= ===============================
1508+
1509+
Example format strings::
1510+
1511+
'b' # blue markers with default shape
1512+
'ro' # red circles
1513+
'g-' # green solid line
1514+
'--' # dashed line with default color
1515+
'k^:' # black triangle_up markers connected by a dotted line
14121516
1413-
kwargs *scalex* and *scaley*, if defined, are passed on to
1414-
:meth:`~matplotlib.axes.Axes.autoscale_view` to determine
1415-
whether the *x* and *y* axes are autoscaled; the default is
1416-
*True*.
14171517
"""
14181518
scalex = kwargs.pop('scalex', True)
14191519
scaley = kwargs.pop('scaley', True)

0 commit comments

Comments
 (0)