簡體   English   中英

將一些文本放入 python 圖中

[英]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()

好的,這個腳本給了我這樣的圖像

python繪圖示例

所以下一步是放置我的數據集而不是隨機矩陣,我知道,但我想在此圖中放置一些軸或文本,並獲得類似此圖像的內容

帶軸的圖像

這是一個使用油漆的非常漂亮的圖像(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM