Say I have the following setup:
import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y = np.exp(x) fig1 = plt.figure() ax1 = fig1.add_subplot(111) ax1.plot(x, y)
I would like to add a title to the plot (or to the subplot).
I tried:
> fig1.title('foo') AttributeError: 'Figure' object has no attribute 'title'
and
> ax1.title('foo') TypeError: 'Text' object is not callable
How can I use the object-oriented programming interface to matplotlib to set these attributes?
More generally, where can I find the hierarchy of classes in matplotlib and their corresponding methods?
A Figure object is the outermost container for a matplotlib graphic, which can contain multiple Axes objects. One source of confusion is the name: an Axes actually translates into what we think of as an individual plot or graph (rather than the plural of “axis,” as we might expect).
figure. The figure module provides the top-level Artist , the Figure , which contains all the plot elements. The following classes are defined SubplotParams control the default spacing of the subplots Figure. Top level container for all plot elements.
The plt. axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt.
use ax1.set_title('foo')
instead
ax1.title
returns a matplotlib.text.Text
object:
In [289]: ax1.set_title('foo') Out[289]: <matplotlib.text.Text at 0x939cdb0> In [290]: print ax1.title Text(0.5,1,'foo')
You can also add a centered title to the figure when there are multiple AxesSubplot
:
In [152]: fig, ax=plt.subplots(1, 2) ...: fig.suptitle('title of subplots') Out[152]: <matplotlib.text.Text at 0x94cf650>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With