日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中的annotate函數使用_python

作者:watermelon12138 ? 更新時間: 2022-07-08 編程語言

python的annotate函數

annotate函數

該函數的詳細參數可調用內置屬性__doc__查看。

	import matplotlib.pyplot as plt
    # plt.annotate(str, xy=data_point_position, xytext=annotate_position, 
    #              va="center",  ha="center", xycoords="axes fraction", 
    #              textcoords="axes fraction", bbox=annotate_box_type, arrowprops=arrow_style)
    # str是給數據點添加注釋的內容,支持輸入一個字符串
    # xy=是要添加注釋的數據點的位置
    # xytext=是注釋內容的位置
    # bbox=是注釋框的風格和顏色深度,fc越小,注釋框的顏色越深,支持輸入一個字典
    # va="center",  ha="center"表示注釋的坐標以注釋框的正中心為準,而不是注釋框的左下角(v代表垂直方向,h代表水平方向)
    # xycoords和textcoords可以指定數據點的坐標系和注釋內容的坐標系,通常只需指定xycoords即可,textcoords默認和xycoords相同
    # arrowprops可以指定箭頭的風格支持,輸入一個字典
    # plt.annotate()的詳細參數可用__doc__查看,如:print(plt.annotate.__doc__)

例1:

	import matplotlib.pyplot as plt
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    plt.annotate('a decision node', (0.1, 0.5), (0.5, 0.1), va="center",  ha="center",
                 xycoords="axes fraction", textcoords="axes fraction", 
                 bbox=dict(boxstyle="sawtooth", fc="0.8"), arrowprops=dict(arrowstyle="<-"))
    plt.show()

結果如下:

在這里插入圖片描述

例2:給注釋和數據點指定不同的坐標系

	import matplotlib.pyplot as plt
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    # 這里指定數據點的坐標系原點在xy軸的左下角,而注釋的坐標系原點在這個圖像(figure)的左下角
    # 所以才會出現注釋內容下移覆蓋了x軸
    plt.annotate('a decision node', (0.1, 0.5), (0.5, 0.1), va="center",  ha="center",
                 xycoords="axes fraction", textcoords="figure fraction", 
                 bbox=dict(boxstyle="sawtooth", fc="0.8"), arrowprops=dict(arrowstyle="<-"))
    plt.show()

結果如下:

在這里插入圖片描述

可視化annotate()函數解析

函數功能:添加圖形內容細節的指向型注釋文本。

調用簽名:

plt.annotate(string, xy=(np.pi/2, 1.0), xytext=((np.pi/2)+0.15, 1,5), weight="bold", color="b", arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b"))
  • string:圖形內容的注釋文本
  • xy:被注釋圖形內容的位置坐標
  • xytext:注釋文本的位置坐標
  • weight:注釋文本的字體粗細風格
  • color:注釋文本的字體顏色
  • arrowprops:指示被注釋內容的箭頭的屬性字典

代碼實現:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.annotate("maximum", xy=(np.pi/2, 1.0), xytext=((np.pi/2)+1.0, .8),
             weight="bold", color="b", 
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b"))
plt.show()

原文鏈接:https://blog.csdn.net/watermelon12138/article/details/88375823

欄目分類
最近更新