matplotlib作图之注释

不写python的日子每天都觉得没啥意思,重新搞起~

matplotlib中图的注释

直接上代码看例子:


import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

#[0,5]区间,间隔0.01

t = np.arange(0.0, 5.0, 0.01)

s = np.cos(2*np.pi*t)

#lw线条宽度2

ax.plot(t, s, lw=2)

"""

'local max'为注释的文本

箭头指向座标系中的点(2, 1)

注释文本位于座标系点(3, 1.5)

箭头样式arrowprops=dict(facecolor='black', shrink=0.05)

"""

ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),

            arrowprops=dict(facecolor='black', shrink=0.05))

#y座标轴(-2,2)范围

ax.set_ylim(-2,2)

#作图

plt.show()

matplotlib作图之注释

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

t = np.arange(0.0, 5.0, 0.01)

s = np.cos(2*np.pi*t)

ax.plot(t, s, lw=2)

"""

此处xycoords='data',xytext=(0.8, 0.95), textcoords='axesfraction'

是定义的注释文本的位置方式。

(0.8, 0.95)表示,注释文本位于座标系的横座标的80%,纵座标的95%位置

"""

ax.annotate('local max', xy=(3, 1),  xycoords='data',

            xytext=(0.8, 0.95), textcoords='axes fraction',

            arrowprops=dict(facecolor='black', shrink=0.05),

            horizontalalignment='right', verticalalignment='top'

            )

ax.set_ylim(-2,2)

plt.show()

matplotlib作图之注释


import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111, polar=True)

r = np.arange(0,1,0.001)

theta = 2*2*np.pi*r

ax.plot(theta, r, color='#ee8d18', lw=3)

ind = 800

thisr, thistheta = r[ind], theta[ind]

ax.plot([thistheta], [thisr], 'o')

ax.annotate('a polar annotation',

            xy=(thistheta, thisr),  

            xytext=(0.05, 0.05),   

            #注释文本位于,相对于figure而言

            textcoords='figure fraction',

            arrowprops=dict(facecolor='black', shrink=0.05),

            #水平对齐方式:左对齐

            horizontalalignment='left',

            #垂直对齐方式:bottom对齐

            verticalalignment='bottom')

plt.show()

matplotlib作图之注释

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章