2016-03-29 19 views
1

Ich versuche, eine Korrelationsplot mit Python setzen zu tun, also mit diesem grundlegenden Beispiel ich beginneeinen Text zu einem Python-Plot

import numpy as np 
import matplotlib.pyplot as plt 

image=np.random.rand(10,10) 
plt.imshow(image) 
plt.colorbar() 
plt.show() 

ok, das Skript gibt ein Bild für mich wie diese

python plot example

so ist der nächste Schritt ist mein Dataset und nicht um eine Zufallsmatrix zu setzen, ich weiß es, aber ich möchte etwas Achse oder Text in diesem Diagramm setzen, und so etwas wie dieses Bild

zu bekommen

image with axis

Es ist ein sehr hübsches Bild mit Farbe (lol), aber jemand kann mir sagen, auf welche Weise ich folgen muss, um etwas wie thik bitte zu tun (wie man es in Google sucht).

Bevor es zu schreiben denke ich in Etiketten, aber ich denke auch, dass ich nur ein Etikett auf jede Achse zuweisen

prost

+0

'annotate' mit Achsen Brucheinheiten den Text bekommen. – tacaswell

Antwort

1

Wie @tcaswell in den Kommentaren gesagt, die Funktion, die Sie verwenden möchten ist annotieren, und die documentation can be found here.

Ich habe ein Beispiel unter Verwendung von Code oben angegeben:

import numpy as np 
import matplotlib.pyplot as plt 

def annotate_axes(x1,y1,x2,y2,x3,y3,text):      
    ax.annotate('', xy=(x1, y1),xytext=(x2,y2),    #draws an arrow from one set of coordinates to the other 
      arrowprops=dict(arrowstyle='<->'),    #sets style of arrow 
      annotation_clip=False)       #This enables the arrow to be outside of the plot 

    ax.annotate(text,xy=(0,0),xytext=(x3,y3),    #Adds another annotation for the text 
       annotation_clip=False) 


fig, ax = plt.subplots() 
image=np.random.rand(10,10) 
plt.imshow(image) 
plt.colorbar() 

#annotate x-axis 
annotate_axes(-0.5,10,4.5,10,2.5,10.5,'A')  # changing these changes the position of the arrow and the text 
annotate_axes(5,10,9.5,10,7.5,10.5,'B') 

#annotate y-axis 
annotate_axes(-1,0,-1,4,-1.5,2,'A') 
annotate_axes(-1,4.5,-1,9.5,-1.5,7.5,'B') 

plt.show() 

Dies dem Bild gibt unten gezeigt:

enter image description here

+0

Danke, es ist sehr nützlich, ich werde es verwenden :) –

Verwandte Themen