@@ -250,18 +250,98 @@ def my_plotter(ax, data1, data2, param_dict):
250
250
#
251
251
# Multiple axes can be added a number of ways, but the most basic is
252
252
# ``plt.subplots()`` as used above. One can achieve more complex layouts,
253
- # with axes spanning columns or rows, using `~.pyplot.subplot_mosaic`:
253
+ # with axes spanning columns or rows, using `~.pyplot.subplot_mosaic`.
254
254
255
255
fig , axd = plt .subplot_mosaic([['upleft' , 'right' ],
256
256
['lowleft' , 'right' ]], constrained_layout = True )
257
257
axd ['upleft' ].set_title ('upleft' )
258
258
axd ['lowleft' ].set_title ('lowleft' )
259
259
axd ['right' ].set_title ('right' )
260
260
261
+ ###############################################################################
262
+ # Matplotlib has quite sophisticated tools for arranging axes: See
263
+ # :doc:`/tutorials/intermediate/arranging_axes` and
264
+ # :doc:`/tutorials/provisional/mosaic`.
265
+ #
266
+ # Working with text
267
+ # =================
268
+ #
269
+ # `~.Axes.text` can be used to add text in an arbitrary location, and
270
+ # `~.Axes.set_xlabel`, `~.Axes.set_ylabel` and `~.Axes.set_title` are used to
271
+ # add text in the indicated locations (see :doc:`/tutorials/text/text_intro` for
272
+ # more discussion)
261
273
274
+ mu , sigma = 100 , 15
275
+ x = mu + sigma * np .random .randn (10000 )
276
+ fig , ax = plt .subplots ()
277
+ # the histogram of the data
278
+ n , bins , patches = ax .hist (x , 50 , density = 1 , facecolor = 'g' , alpha = 0.75 )
262
279
280
+ ax .set_xlabel ('Smarts' )
281
+ ax .set_ylabel ('Probability' )
282
+ ax .set_title ('Histogram of IQ' )
283
+ ax .text (60 , .025 , r'$\mu=100,\ \sigma=15$' )
284
+ ax .axis ([40 , 160 , 0 , 0.03 ])
285
+ ax .grid (True )
286
+ plt .show ()
263
287
288
+ ###############################################################################
289
+ # All of the `~.pyplot.text` functions return a `matplotlib.text.Text`
290
+ # instance. Just as with lines above, you can customize the properties by
291
+ # passing keyword arguments into the text functions::
292
+ #
293
+ # t = ax.set_xlabel('my data', fontsize=14, color='red')
294
+ #
295
+ # These properties are covered in more detail in :doc:`/tutorials/text/text_props`.
296
+ #
297
+ # Using mathematical expressions in text
298
+ # --------------------------------------
299
+ #
300
+ # matplotlib accepts TeX equation expressions in any text expression.
301
+ # For example to write the expression :math:`\sigma_i=15` in the title,
302
+ # you can write a TeX expression surrounded by dollar signs::
303
+ #
304
+ # ax.set_title(r'$\sigma_i=15$')
264
305
#
306
+ # The ``r`` preceding the title string signifies that the string is a
307
+ # *raw* string and not to treat backslashes as python escapes.
308
+ # Matplotlib has a built-in TeX expression parser and
309
+ # layout engine, and ships its own math fonts -- for details see
310
+ # :doc:`/tutorials/text/mathtext`. Thus you can use mathematical text across
311
+ # platforms without requiring a TeX installation. For those who have LaTeX and
312
+ # dvipng installed, you can also use LaTeX to format your text and
313
+ # incorporate the output directly into your display figures or saved
314
+ # postscript -- see :doc:`/tutorials/text/usetex`.
315
+ #
316
+ #
317
+ # Annotations
318
+ # -----------
319
+ #
320
+ # We can also annotate points on a plot. There are
321
+ # two points to consider: the location being annotated represented by
322
+ # the argument ``xy`` and the location of the text ``xytext``. Both of
323
+ # these arguments are ``(x, y)`` tuples:
324
+
325
+ fig , ax = plt .subplots ()
326
+
327
+ t = np .arange (0.0 , 5.0 , 0.01 )
328
+ s = np .cos (2 * np .pi * t )
329
+ line , = ax .plot (t , s , lw = 2 )
330
+
331
+ ax .annotate ('local max' , xy = (2 , 1 ), xytext = (3 , 1.5 ),
332
+ arrowprops = dict (facecolor = 'black' , shrink = 0.05 ),
333
+ )
334
+
335
+ ax .set_ylim (- 2 , 2 )
336
+ plt .show ()
337
+
338
+ ###############################################################################
339
+ # In this basic example, both the ``xy`` (arrow tip) and ``xytext``
340
+ # locations (text location) are in data coordinates. There are a
341
+ # variety of other coordinate systems one can choose -- see
342
+ # :ref:`annotations-tutorial` and :ref:`plotting-guide-annotation` for
343
+ # details. More examples can be found in
344
+ # :doc:`/gallery/text_labels_and_annotations/annotation_demo`.
265
345
#
266
346
# More reading
267
347
# ============
0 commit comments