plt8_annotation.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 8 - annotation
  5. """
  6. Please note, this script is for python3+.
  7. If you are using python2+, please modify it accordingly.
  8. Tutorial reference:
  9. http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
  10. Mathematical expressions:
  11. http://matplotlib.org/users/mathtext.html#mathtext-tutorial
  12. """
  13. import matplotlib.pyplot as plt
  14. import numpy as np
  15. x = np.linspace(-3, 3, 50)
  16. y = 2*x + 1
  17. plt.figure(num=1, figsize=(8, 5),)
  18. plt.plot(x, y,)
  19. ax = plt.gca()
  20. ax.spines['right'].set_color('none')
  21. ax.spines['top'].set_color('none')
  22. ax.spines['top'].set_color('none')
  23. ax.xaxis.set_ticks_position('bottom')
  24. ax.spines['bottom'].set_position(('data', 0))
  25. ax.yaxis.set_ticks_position('left')
  26. ax.spines['left'].set_position(('data', 0))
  27. x0 = 1
  28. y0 = 2*x0 + 1
  29. plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)
  30. plt.scatter([x0, ], [y0, ], s=50, color='b')
  31. # method 1:
  32. #####################
  33. plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
  34. textcoords='offset points', fontsize=16,
  35. arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
  36. # method 2:
  37. ########################
  38. plt.text(-3.7, 3, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
  39. fontdict={'size': 16, 'color': 'r'})
  40. plt.show()