简体   繁体   English

将一些文本放入 python 图中

[英]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我正在尝试使用 python 绘制相关图,所以我从这个基本示例开始

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).这是一个使用油漆的非常漂亮的图像(lol),但有人可以告诉我我需要按照什么方式来做类似 thik 的事情(如何在谷歌中搜索它)。

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 .正如@tcaswell 在评论中所说,你要使用的函数是 annotate,文档可以在这里找到

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:这给出了如下所示的图像:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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