1

I'm trying to do a correlation plot using python, so I'm starting with this basic example

import numpy as np
import matplotlib.pyplot as plt

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

ok, this script give to me an image like this

python plot example

so the next step is to put my dataset and not a random matrix, i know it, but I want to put some axis or text in this plot, and to get something like this image

image with axis

It is a very pretty image using paint (lol), but someone can say me what way I need to follow to do something like thik please (how to search it in google).

Before to post it I think in labels, but also I think that I can assign only one label to each axis

cheers

1
  • annotate with axes fraction units will get the text. Commented Mar 29, 2016 at 4:11

1 Answer 1

1

As @tcaswell said in the comments, the function you want to use is annotate, and the documentation can be found here.

I've given an example below using your code above:

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

This give the image shown below:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it is very usefull, I will use it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.