[英]putting some text to a python plot
我正在尝试使用 python 绘制相关图,所以我从这个基本示例开始
import numpy as np
import matplotlib.pyplot as plt
image=np.random.rand(10,10)
plt.imshow(image)
plt.colorbar()
plt.show()
好的,这个脚本给了我这样的图像
所以下一步是放置我的数据集而不是随机矩阵,我知道,但我想在此图中放置一些轴或文本,并获得类似此图像的内容
这是一个使用油漆的非常漂亮的图像(lol),但有人可以告诉我我需要按照什么方式来做类似 thik 的事情(如何在谷歌中搜索它)。
在发布之前,我认为在标签中,但我认为我只能为每个轴分配一个标签
干杯
正如@tcaswell 在评论中所说,你要使用的函数是 annotate,文档可以在这里找到。
我在下面使用您上面的代码给出了一个示例:
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()
这给出了如下所示的图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.