Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure and axes methods in matplotlib

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?

like image 974
Amelio Vazquez-Reina Avatar asked Feb 19 '14 15:02

Amelio Vazquez-Reina


People also ask

What is matplotlib figure and axes?

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).

What is a figure in matplotlib?

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.

What does PLT axis () do?

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.


1 Answers

use ax1.set_title('foo') instead

ax1.title returns a matplotlib.text.Textobject:

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> 
like image 198
zhangxaochen Avatar answered Sep 19 '22 14:09

zhangxaochen