分享

matplotlib作图之注释

 大邓的Python 2021-02-23

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

matplotlib中图的注释

直接上代码看例子:

  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3. fig = plt.figure()

  4. ax = fig.add_subplot(111)

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

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

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

  8. #lw线条宽度2

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

  10. """

  11. 'local max'为注释的文本

  12. 箭头指向坐标系中的点(2, 1)

  13. 注释文本位于坐标系点(3, 1.5)

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

  15. """

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

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

  18. #y坐标轴(-2,2)范围

  19. ax.set_ylim(-2,2)

  20. #作图

  21. plt.show()

  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3. fig = plt.figure()

  4. ax = fig.add_subplot(111)

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

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

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

  8. """

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

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

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

  12. """

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

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

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

  16.            horizontalalignment='right', verticalalignment='top'

  17.            )

  18. ax.set_ylim(-2,2)

  19. plt.show()

  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3. fig = plt.figure()

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

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

  6. theta = 2*2*np.pi*r

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

  8. ind = 800

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

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

  11. ax.annotate('a polar annotation',

  12.            xy=(thistheta, thisr),  

  13.            xytext=(0.05, 0.05),  

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

  15.            textcoords='figure fraction',

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

  17.            #水平对齐方式:左对齐

  18.            horizontalalignment='left',

  19.            #垂直对齐方式:bottom对齐

  20.            verticalalignment='bottom')

  21. plt.show()

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多