0

I'm putting together some matplotlib charts but I'd like to put proper data source attribution in a text box just below the x-axis. I've looked at a few examples using text() but can't get any to work.

I'm looking to add something like the text in the grey box in this image (taken from http://www.futurile.net/2016/03/01/text-handling-in-matplotlib/, which frustratingly doesn't include the code to produce that plot!):

enter image description here

I feel like this must be a relatively common thing, but I'm a bit stumped. How do I add that text using matplotlib?

Chris Withers
  • 10,837
  • 4
  • 33
  • 51

1 Answers1

1

In my understanding you need to place your text with respect to figure coordinates, for that you have to use the fig.text method (or alternatively plt.figtext) to place text wrt figure coordinates (NB: in figure coordinates (0, 0) is the left bottom and (1, 1) is the right top).

For the options that you can pass to all the methods that instantiate a Matplotlib Text object, please reference the fine docs As an example of its use

In [34]: import numpy as np 
    ...: import matplotlib 
    ...: import matplotlib.pyplot as plt 
    ...:  
    ...: t = np.linspace(0, 2*np.pi, 629) 
    ...: fig, ax = plt.subplots() 
    ...: ax.plot(t, np.real(np.exp(t*1j))) 
    ...: ax.set_xlabel('x/π') 
    ...: text = fig.text(0.50, 0.02, 
    ...:  'Leonard Euler (1748) Chapter 8: On transcending quantities arising from the circle', 
    ...:  horizontalalignment='center', wrap=True ) 
    ...: fig.tight_layout(rect=(0,.05,1,1)) 
    ...: plt.show()                                                                                                             

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Look, I don't really understand your need to be so rude in the way you answer. The edits I suggested highlighted the helpful parts of your answer, but you've gone out of your way to change the answer back to its original rude form. Thank for the help, but good luck to you, you're going to need it. – Chris Withers Jun 18 '20 at 06:21