plt6_ax_setting2.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. # 6 - axis setting
  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. """
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. x = np.linspace(-3, 3, 50)
  14. y1 = 2*x + 1
  15. y2 = x**2
  16. plt.figure()
  17. plt.plot(x, y2)
  18. # plot the second curve in this figure with certain parameters
  19. plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
  20. # set x limits
  21. plt.xlim((-1, 2))
  22. plt.ylim((-2, 3))
  23. # set new ticks
  24. new_ticks = np.linspace(-1, 2, 5)
  25. plt.xticks(new_ticks)
  26. # set tick labels
  27. plt.yticks([-2, -1.8, -1, 1.22, 3],
  28. ['$really\ bad$', '$bad$', '$normal$', '$good$', '$really\ good$'])
  29. # to use '$ $' for math text and nice looking, e.g. '$\pi$'
  30. # gca = 'get current axis'
  31. ax = plt.gca()
  32. ax.spines['right'].set_color('none')
  33. ax.spines['top'].set_color('none')
  34. ax.xaxis.set_ticks_position('bottom')
  35. # ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]
  36. ax.spines['bottom'].set_position(('data', 0))
  37. # the 1st is in 'outward' | 'axes' | 'data'
  38. # axes: percentage of y axis
  39. # data: depend on y data
  40. ax.yaxis.set_ticks_position('left')
  41. # ACCEPTS: [ 'left' | 'right' | 'both' | 'default' | 'none' ]
  42. ax.spines['left'].set_position(('data',0))
  43. plt.show()