|
100 | 100 | # input. Classes that are similar to arrays ('array-like') such as `pandas`
|
101 | 101 | # data objects and `numpy.matrix` may not work as intended. Common convention
|
102 | 102 | # is to convert these to `numpy.array` objects prior to plotting.
|
103 |
| -# |
104 |
| -# For example, to convert a `pandas.DataFrame` :: |
105 |
| -# |
106 |
| -# a = pandas.DataFrame(np.random.rand(4, 5), columns = list('abcde')) |
107 |
| -# a_asarray = a.values |
108 |
| -# |
109 |
| -# and to convert a `numpy.matrix` :: |
| 103 | +# For example, to convert a `numpy.matrix` :: |
110 | 104 | #
|
111 | 105 | # b = np.matrix([[1, 2], [3, 4]])
|
112 | 106 | # b_asarray = np.asarray(b)
|
113 | 107 | #
|
| 108 | +# There are some instances where you have data in a format that lets you |
| 109 | +# access particular variables with strings. For example, a *dict*, a |
| 110 | +# `numpy.recarray`, or a `pandas.DataFrame`. Matplotlib allows you provide |
| 111 | +# the ``data`` keyword argument and generate plots passing the strings |
| 112 | +# corresponding to these *x* and *y* variables. |
| 113 | + |
| 114 | +data = {'a': np.arange(50), |
| 115 | + 'c': np.random.randint(0, 50, 50), |
| 116 | + 'd': np.random.randn(50)} |
| 117 | +data['b'] = data['a'] + 10 * np.random.randn(50) |
| 118 | +data['d'] = np.abs(data['d']) * 100 |
| 119 | + |
| 120 | +fig, ax = plt.subplots() |
| 121 | +ax.scatter('a', 'b', c='c', s='d', data=data) |
| 122 | +ax.set_xlabel('entry a') |
| 123 | +ax.set_ylabel('entry b') |
| 124 | + |
| 125 | +############################################################################## |
114 | 126 | # .. _coding_styles:
|
115 | 127 | #
|
116 | 128 | # The object-oriented interface and the pyplot interface
|
@@ -229,6 +241,28 @@ def my_plotter(ax, data1, data2, param_dict):
|
229 | 241 | # Matplotlib has one at `mpl-cookiecutter
|
230 | 242 | # <https://github.com/matplotlib/matplotlib-extension-cookiecutter>`_
|
231 | 243 | #
|
| 244 | +# Working with multiple figures and axes |
| 245 | +# ====================================== |
| 246 | +# |
| 247 | +# You can open multiple figures with multiple calls to |
| 248 | +# ``fig = plt.figure()`` or ``fig2, ax = plt.subplots()``. By keeping the |
| 249 | +# object references you can add artists to either figure. |
| 250 | +# |
| 251 | +# Multiple axes can be added a number of ways, but the most basic is |
| 252 | +# ``plt.subplots()`` as used above. One can achieve more complex layouts, |
| 253 | +# with axes spanning columns or rows, using `~.pyplot.subplot_mosaic`: |
| 254 | + |
| 255 | +fig, axd = plt.subplot_mosaic([['upleft', 'right'], |
| 256 | + ['lowleft', 'right']], constrained_layout=True) |
| 257 | +axd['upleft'].set_title('upleft') |
| 258 | +axd['lowleft'].set_title('lowleft') |
| 259 | +axd['right'].set_title('right') |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | +# |
| 265 | +# |
232 | 266 | # More reading
|
233 | 267 | # ============
|
234 | 268 | #
|
|
0 commit comments