简体   繁体   中英

putting some text to a python plot

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绘图示例

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

带轴的图像

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

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM