2016-07-16 22 views
6

Ich möchte mehr feinkörnigen Gitter auf einem geplotteten Diagramm hinzufügen. Das Problem besteht darin, dass alle Beispiele Zugriff auf das Achsenobjekt erfordern. Ich möchte ein bestimmtes Raster zu bereits geplotteten Graphen hinzufügen (von innen ipython).ipython: Zugriff auf aktuelle Figur()

Wie bekomme ich Zugriff auf die aktuelle Figur und Achse in ipython?

Antwort

6

Mit dem plt? Beispiel (unter der Annahme ipython --pylab)

In [44]: x=np.arange(0,5,.1) 
In [45]: y=np.sin(x) 
In [46]: plt.plot(x,y) 
Out[46]: [<matplotlib.lines.Line2D at 0xb09418cc>] 

Displays figure 1; erhalten, dessen Griff mit:

In [47]: f=plt.figure(1) 

In [48]: f 
Out[48]: <matplotlib.figure.Figure at 0xb17acb2c> 

und eine Liste seiner Achsen:

In [49]: f.axes 
Out[49]: [<matplotlib.axes._subplots.AxesSubplot at 0xb091198c>] 

auf das Gitter drehen für den Strom (und einzige) Achse:

In [51]: a=f.axes[0] 
In [52]: a.grid(True) 

Ich habe nicht Benutzte die Plt in einer Weile, also fand dieses Zeug, indem Sie einfach die Handlung und die Suche nach der Tab-Vervollständigung und? für wahrscheinlich Zeug. Ich bin mir ziemlich sicher, dass dies auch in der plt Dokumentation verfügbar ist.

Oder Sie können das Bild zuerst erstellen, und hängen an ihrem Griff

In [53]: fig=plt.figure() 
In [55]: ax1=fig.add_subplot(2,1,1) 
In [56]: ax2=fig.add_subplot(2,1,2) 

In [57]: plt.plot(x,y) 
Out[57]: [<matplotlib.lines.Line2D at 0xb12ed5ec>] 

In [58]: fig.axes 
Out[58]: 
[<matplotlib.axes._subplots.AxesSubplot at 0xb0917e2c>, 
<matplotlib.axes._subplots.AxesSubplot at 0xb17a35cc>] 

Und es gibt gcf und gca (get aktuelle Zahl/Achse). Wie in MATLAB, wenn mein Speicher korrekt ist.

In [68]: plt.gca() 
Out[68]: <matplotlib.axes._subplots.AxesSubplot at 0xb17a35cc> 
In [66]: plt.gcf() 
Out[66]: <matplotlib.figure.Figure at 0xb091eeec> 

(diese sind in der Seitenleiste Verbindung verwendet: Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplot)

18

plt.gcf() aktuelle Zahl

plt.gca() zu erhalten aktuelle Achse

zu erhalten