2014-02-19 21 views
9

Say I haben die folgende Einstellung:Achsen Methoden in matplotlib

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) 

Ich mag würde ein Titel auf die Zeichnung hinzuzufügen (oder zu dem subplot).

Ich habe versucht:

> fig1.title('foo') 
AttributeError: 'Figure' object has no attribute 'title' 

und

> ax1.title('foo') 
TypeError: 'Text' object is not callable 

Wie kann ich die objektorientierte Programmierung Schnittstelle verwenden, um matplotlib diese Attribute zu setzen?

Allgemeiner, wo finde ich die Hierarchie der Klassen in Matplotlib und ihre entsprechenden Methoden?

Antwort

22

Verwendung ax1.set_title('foo') statt

ax1.title gibt ein matplotlib.text.Text Objekt:

In [289]: ax1.set_title('foo') 
Out[289]: <matplotlib.text.Text at 0x939cdb0> 

In [290]: print ax1.title 
Text(0.5,1,'foo') 

Sie können auch einen zentrierten Titel auf die Figur hinzufügen, wenn es mehrere AxesSubplot:

In [152]: fig, ax=plt.subplots(1, 2) 
    ...: fig.suptitle('title of subplots') 
Out[152]: <matplotlib.text.Text at 0x94cf650> 
+0

Auf meinem System (Ubuntu 13.10, ipython 1.1.0, python 2.7.5, matplotlib 1.2.1) Ich brauche eine 'fig1.show()' nach dem 'ax1.set_title (...) c alles um sichtbare Wirkung zu erzielen. Meine Vermutung ist, dass dies Standardverhalten ist. –

+0

@RoryYorke yep, Sie müssen standardmäßig show() aufrufen. – zhangxaochen

+1

FWIW, 'figure' hat die' suptitle' Methode, mit der Sie einen Titel über mehrere Unterplots setzen können. – physicsmichael

Verwandte Themen