Skip to content

Commit 1375f72

Browse files
committed
improve docstring of Axes.plot
1 parent 3c1f423 commit 1375f72

File tree

1 file changed

+199
-103
lines changed

1 file changed

+199
-103
lines changed

lib/matplotlib/axes/_axes.py

+199-103
Original file line numberDiff line numberDiff line change
@@ -1288,132 +1288,228 @@ 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 lines and/or a series of markers. This is also known as line plot
1292+
or XY (scatter) plot respectively.
12961293
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
1294+
There are different ways how you can use the interface:
13011295
1302-
If *x* and/or *y* is 2-dimensional, then the corresponding columns
1303-
will be plotted.
1296+
**1. Basic interface**
13041297
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.
1298+
::
13101299
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::
1300+
plot([x,] y)
13131301
1314-
a.plot(x1, y1, 'g^', x2, y2, 'g-')
1302+
The coordinates of the points or line nodes are given by *x*, *y*.
1303+
Their properties are controlled by keyword arguments, which are
1304+
`~.Line2D` properties.
13151305
1316-
Return value is a list of lines that were added.
1306+
To plot multiple sets of data, call `.plot` multiple times, or
1307+
alternatively use 2-dimensional datasets.
13171308
1318-
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-
================ ===============================
1309+
Typical example::
13551310
1311+
plot(x, y, linewidth='2', color='purple')
13561312
1357-
The following color abbreviations are supported:
13581313
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-
========== ========
1314+
**2. MATLAB-style interface**
13711315
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``.
1316+
This interface is suitable for simpler use cases, e.g. when quickly
1317+
drawing something in an interactive session.
13781318
1379-
Line styles and colors are combined in a single format string, as in
1380-
``'bo'`` for blue circles.
1319+
::
13811320
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::
1321+
plot([x,] y, [fmt,])
1322+
plot([x,] y, [fmt,], [x2,] y2, [fmt2,], ...) # multiple data sets
13861323
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()
1324+
You can define basic formatting like color, marker and linestyle using
1325+
a format string *fmt*. The formats are explained in the *Notes*
1326+
section below.
13911327
1392-
If you make multiple lines with one plot command, the kwargs
1393-
apply to all those lines, e.g.::
1328+
Multiple sets of *x*, *y*, *[fmt]* groups can be given. In this case,
1329+
any additional keyword arguments apply to all data sets.
13941330
1395-
plot(x1, y1, x2, y2, antialiased=False)
1331+
Typical examples::
13961332
1397-
Neither line will be antialiased.
1333+
plot(x, y, 'ro') # plot x and y using blue circle markers
1334+
plot(x1, y1, 'g^', x2, y2, 'g-') # plot two sets of data
13981335
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::
14031336
1404-
plot(x, y, color='green', linestyle='dashed', marker='o',
1405-
markerfacecolor='blue', markersize=12).
1337+
**3. Labelled data interface**
14061338
1407-
See :class:`~matplotlib.lines.Line2D` for details.
1339+
For objects with labelled data (i.e. data that can be accessed by
1340+
index ``obj['y']``) you can specify the labels to plot and provide
1341+
the object in the *data* parameter::
14081342
1409-
The kwargs are :class:`~matplotlib.lines.Line2D` properties:
1343+
plot(ylabel, data=obj)
1344+
plot(xlabel, ylabel, fmt, data=obj)
14101345
1411-
%(Line2D)s
1346+
All indexable objects are supported. This could e.g. be a `dict`, a
1347+
`pandas.DataFame` or a structured numpy array.
1348+
1349+
.. note::
1350+
The signature ``plot(xlabel, ylabel, data=obj)`` without a format
1351+
string is not supported because it would be ambiguous with
1352+
``plot(ylabel, fmt, data=obj)``. In such a case, you can use an
1353+
empty format string::
1354+
1355+
plot(xlabel, ylabel, '', data=obj)
1356+
1357+
Typical example::
1358+
1359+
plot('year', 'happiness', 'b-', data=world_happiness_report)
1360+
1361+
Parameters
1362+
----------
1363+
x, y, x2, y2, ... : array-like or scalar
1364+
The horizontal / vertical coordinates of the data points.
1365+
*x* values are optional. If not given, they default to
1366+
``[0, ..., N-1]``.
1367+
1368+
Commonly, these parameters are arrays of length N. However,
1369+
scalars are supported as well (equivalent to an array with
1370+
constant value).
1371+
1372+
The parameters can also be 2-dimensional. Then, the columns
1373+
represent separate data sets.
1374+
1375+
fmt : str, optional
1376+
A format string, e.g. 'ro' for red circles. See the *Notes*
1377+
section for a full description of the format strings.
1378+
1379+
Format strings are just an abbreviation for quickly setting
1380+
basic line properties. All of these and more can also be
1381+
controlled by keyword arguments.
1382+
1383+
data : indexable object, optional
1384+
The data object for using the labelled data interface.
1385+
1386+
Other Parameters
1387+
----------------
1388+
scalex, scaley : bool, optional, default: True
1389+
These parameters determined if the view limits are adapted to
1390+
the data limits. The values are passed on to `autoscale_view`.
1391+
1392+
**kwargs : `~.Line2D` properties, optional
1393+
*kwargs* are used to specify roperties like a line label (for
1394+
auto legends), linewidth, antialiasing, marker face color.
1395+
Example::
1396+
1397+
plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2)
1398+
plot([1,2,3], [1,4,9], 'rs', label='line 2')
1399+
1400+
If you make multiple lines with one plot command, the kwargs
1401+
apply to all those lines, e.g.::
1402+
1403+
plot(x1, y1, x2, y2, antialiased=False)
1404+
1405+
Here is a list of available `~.Line2D` properties:
1406+
1407+
%(Line2D)s
1408+
1409+
Returns
1410+
-------
1411+
lines
1412+
A list of `~.Line2D` objects that were added.
1413+
1414+
1415+
See Also
1416+
--------
1417+
scatter : XY scatter plot with markers of variing size (
1418+
sometimes also called bubble chart).
1419+
1420+
1421+
1422+
Notes
1423+
-----
1424+
By default, each line is assigned a different style specified by a
1425+
'style cycle'. The *fmt* and line property parameters are only
1426+
neccessary if you want explicit deviations from these defaults.
1427+
Alternatively, you can also change the style cycle using the
1428+
``'axes.prop_cycle'`` rcParam.
1429+
1430+
1431+
**Format Strings**
1432+
1433+
A format string consists of a part for color, marker and line::
1434+
1435+
fmt = '[color][marker][line]'
1436+
1437+
Each of them is optional. If not provided, the value from the style
1438+
cycle is used. Exception: If ``line`` is given, but no ``marker``,
1439+
the data will be a line without markers.
1440+
1441+
**Colors**
1442+
1443+
The following color abbreviations are supported:
1444+
1445+
============= ===============================
1446+
character color
1447+
============= ===============================
1448+
``'b'`` blue
1449+
``'g'`` green
1450+
``'r'`` red
1451+
``'c'`` cyan
1452+
``'m'`` magenta
1453+
``'y'`` yellow
1454+
``'k'`` black
1455+
``'w'`` white
1456+
============= ===============================
1457+
1458+
In addition, you can specify colors in many weird and
1459+
wonderful ways, including full names (``'green'``), hex
1460+
strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
1461+
grayscale intensities as a string (``'0.8'``). Of these, the
1462+
string specifications can be used in place of a ``fmt`` group,
1463+
but the tuple forms can be used only as ``kwargs``.
1464+
1465+
**Markers**
1466+
1467+
============= ===============================
1468+
character description
1469+
============= ===============================
1470+
``'.'`` point marker
1471+
``','`` pixel marker
1472+
``'o'`` circle marker
1473+
``'v'`` triangle_down marker
1474+
``'^'`` triangle_up marker
1475+
``'<'`` triangle_left marker
1476+
``'>'`` triangle_right marker
1477+
``'1'`` tri_down marker
1478+
``'2'`` tri_up marker
1479+
``'3'`` tri_left marker
1480+
``'4'`` tri_right marker
1481+
``'s'`` square marker
1482+
``'p'`` pentagon marker
1483+
``'*'`` star marker
1484+
``'h'`` hexagon1 marker
1485+
``'H'`` hexagon2 marker
1486+
``'+'`` plus marker
1487+
``'x'`` x marker
1488+
``'D'`` diamond marker
1489+
``'d'`` thin_diamond marker
1490+
``'|'`` vline marker
1491+
``'_'`` hline marker
1492+
============= ===============================
1493+
1494+
**Line Styles**
1495+
1496+
============= ===============================
1497+
character description
1498+
============= ===============================
1499+
``'-'`` solid line style
1500+
``'--'`` dashed line style
1501+
``'-.'`` dash-dot line style
1502+
``':'`` dotted line style
1503+
============= ===============================
1504+
1505+
Example format strings::
1506+
1507+
'b' # blue markers with default shape
1508+
'ro' # red circles
1509+
'g-' # green solid line
1510+
'--' # dashed line with default color
1511+
'k^:' # black triangle_up markers connected by a dotted line
14121512
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*.
14171513
"""
14181514
scalex = kwargs.pop('scalex', True)
14191515
scaley = kwargs.pop('scaley', True)

0 commit comments

Comments
 (0)