31

Is there a function for drawing a caption box underneath a figure/graph using matplotlib? I have searched google and haven't found any such function.

Captionbox example

something like what is shown in the image would be great.

Serenity
  • 35,289
  • 20
  • 120
  • 115
Anake
  • 7,201
  • 12
  • 45
  • 59

2 Answers2

37

Use pyplot.text. Here is some sample code:

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(0,3,.25)
y = np.sin(x)
txt = '''
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
    culpa qui officia deserunt mollit anim id est laborum.'''

fig = plt.figure()
ax1 = fig.add_axes((.1,.4,.8,.5))
ax1.bar(x,y,.2)
fig.text(.1,.1,txt)
plt.show()

It produces this: enter image description here

If you want automatic word-wrapping, have a look at this post.

I'm not sure I can help you get it full-justified.

Community
  • 1
  • 1
Paul
  • 42,322
  • 15
  • 106
  • 123
  • 1
    If you also want to have part of the caption bold (I wanted the 'Figure 4.' in bold font), then things become a little more problematic. I followed the advice here http://stackoverflow.com/questions/8376335/styling-part-of-label-in-legend-in-matplotlibgcchgjhghfjghjgfhjfghjgfhjgfjfghjfghj, but it ended up being a 1GB install on Ubuntu and I needed to adjust all my text boxes accordingly. Instead I created two figure text objects and played around A LOT with the placement to get them where I wanted. I would recommend not going for bold unless you come up with a smarter way of doing things. – joelostblom Apr 29 '14 at 02:14
  • 2
    Typically, one would want a caption in terms of figure coordinates (`figtext`), not plot coordinates (`text`). – tom10 Oct 13 '15 at 22:23
  • 4
    This doesn't work for subplots, how would I go about this – Max May 05 '16 at 15:29
  • @tom10 `help(fig.text)` indicates that this function places the text in figure "relative 0-1 coords". I think you are confusing `fig.text` with `ax.text`. – Paul May 06 '16 at 16:24
  • @Max "this doesn't work" isn't very specific. What error/behavior are you getting? This should place text at `(.1,.1)` regardless of which (if any) axes are in the figure. It does not automatically place them under the axis if this is your issue. You have to manually adjust the text location with the `text()` `x` and `y` parameters. – Paul May 06 '16 at 16:28
  • 2
    In Matplotlib version 1.5.3 you can get text wrapping using the `wrap='True'` flag – Kaushik Ghose Nov 01 '16 at 01:20
1

You can use the legend function (pylab.legend). If you want it outside the axes, you can pass to it a specific location (loc keyword argument).

Edit: The legend function takes a title argument, which might help getting what you want. However, for a caption without any legend, Paul's answer is more suited.

Gael Varoquaux
  • 2,466
  • 2
  • 24
  • 12